I need to validate input string which should be in the below format:
<2_upper_case_letters><"-"><2_upper_case_letters><14-digit number><1_uppercase_letter>
Ex: RX-EZ12345678912345B
I tried something like this ^[IN]-?[A-Z]{0,2}?\\d{0,14}[A-Z]{0,1} but its not giving the expected result.
Any help will be appreciated.
Thanks
Your biggest problem is the [IN] at the beginning, which matches only one letter, and only if it's I or N. If you want to match two of any letters, use [A-Z]{2}.
Once you fix that, your regex will still only match RX-E. That's because [A-Z]{0,2}? starts out trying to consume nothing, thanks to the reluctant quantifier, {0,2}?. Then \d{0,14} matches zero digits, and [A-Z]{0,1} greedily consumes the E.
If you want to match exactly 2 letters and 14 digits, use [A-Z]{2} and \d{14}. And since you're validating the string, you should end the regex with the end anchor, $. Result:
^[A-Z]{2}-[A-Z]{2}\d{14}[A-Z]$
...or, as a Java string literal:
"^[A-Z]{2}-[A-Z]{2}\\d{14}[A-Z]$"
As #nhahtdh observed, you don't really have to use the anchors if you're using Java's matches() method to apply the regex, but I recommend doing so anyway. It communicates your intent better, and it makes the regex portable, in case you have to use it in a different flavor/context.
EDIT: If the first two characters should be exactly IN, it would be
^IN-[A-Z]{2}\d{14}[A-Z]$
Simply translating your requirements into a java regex:
"^[A-Z]{2}-[A-Z]{2}\\d{14}[A-Z]$"
This will allow you to use:
if (!input.matches("^[A-Z]{2}-[A-Z]{2}\\d{14}[A-Z]$")) {
// do something because input is invalid
}
Not sure what you are trying to do at the beginning of your current regex.
"^[A-Z]{2}-[A-Z]{2}\\d{14}[A-Z]$"
The regex above will strictly match the input string as you specified. If you use matches function, ^ and $ may be omitted.
Since you want exact number of repetitions, you should specify it as {<number>} only. {<number>,<number>} is used for variable number of repetitions. And ? specify that the token before may or may not appear - if it must be there, then specifying ? is incorrect.
^[A-Z]{2}-[A-Z]{2}\\d{14}[A-Z]$
This should solve your purpose. You can confirm it from here
This should solve your problem. Check out the validity here
^[A-Z]{2}-[A-Z]{2}[0-9]{14}[A-Z]$
^([A-Z]{2,2}[-]{1,1}[A-Z]{2,2}[0-9]{14,14}[A-Z]{1,1}){1,1}$
Related
I am trying to implement an algorithm on java and I need a way to match a pattern where I find any character (lets name it a) then the character 'X' and then the same character a from before. Initial thought was regex, although after some time failing to find a way to do that I am thinking of iterating through all characters and checking them one by one...
But before that if anyone could help, I need something so that ( "AXA", "EXE", "RXR", etc) would match while ("AXB", "EXA", "TXX", etc) would not.
Tried using something like ".X." but of course failed as it matched anything before and after 'X'...
Is there a way to match something like that ?
Capture the leading char, and use a back reference:
(.)X\1
See live demo.
Note that in java you need to use 2 slashes to make a literal slash:
"AXA".matches("(.)X\\1") // true
This is maybe the 100+1 question regarding regex optional suffixes on SO, but I didn't find any, that could help me :(
I need to extract a part of string from the common pattern:
prefix/s/o/m/e/t/h/i/n/g/suffix
using a regular expression. The prefix is constant and the suffix may not appear at all, so prefix/(.+)/suffix doesn't meet my requirements. Pattern prefix/(.+)(?:/suffix)? returns s/o/m/e/t/h/i/n/g/suffix. The part (?:/suffix)? must be somehow more greedy.
I want to get s/o/m/e/t/h/i/n/g from these input strings:
prefix/s/o/m/e/t/h/i/n/g/suffix
prefix/s/o/m/e/t/h/i/n/g/
prefix/s/o/m/e/t/h/i/n/g
Thanks in advance!
Try
prefix\/(.+?)\/?(?:suffix|$)
The regex need to know when the match is done, so match either suffix or end of line ($), and make the capture non greedy.
See it here at regex101.
Try prefix(.*?)(?:/?(?:suffix|$)) if there are characters allowed before prefix of after suffix.
This requires the match to be as short as possible (reluctant quantifier) and be preceeded by one of 3 things: a single slash right before the end of the input, /suffix or the end of the input. That would match /s/o/m/e/t/h/i/n/g in the test cases you provided but would match more for input like prefix/s/o/m/e/t/h/i/n/g/suff (which is ok IMO since you don't know whether /suff is meant to be part of the match or a typo in the suffix).
Is it possible to use only a regex (no additional code!) for matching the nth match? For example:
"CAR" - "TRAIN" - "BOAT" - "BICYCLE"
Now I only want to match the BOAT, regex for matching would be "[A-Z]+" however this also matches the first, second and fourth.
Does anyone have a pure regex solution for this? I need this because I can't change the code that uses the regex, but I can provide a regex.
Best regards,
Robin
I think this lookbehind should do it:
(?<=^("[A-Z]+"[\s-]+){2})"[A-Z]+"
It matches a word that comes two words after the start of the string
If I understood you right and you're putting multiple strings into one regexp, string by string, then no, this is not possible.
The regular expression itself does not have memory which lasts longer than matching time,
so if you're matching one thing and after that another thing, there's no information of the first.
(?!(\"[A-Z]\"\s-\s){2})(\"[A-Z]\") - where the {2} means which index you want.
The only problem with it is that it returns every match after the specified index as well. You could perform the match and return the first result.
Tested it using regexpal with your example.
Well I have this code:
String replacedItemName = ItemDefinitions.getItemDefinitions(usedWith).getName().replaceAll("\\(.\\)", "(6)");
Is \\(.\\) the correct regex to replace anyting in the brackets of the item name? (Java)
I would suggest to use replaceAll("(?<=\\().*?(?=\\))", "6");. See here
Almost, you forgot a plus (one-or-more) after your dot. Without the plus, the dot only matches one character.
\(.+\)
However, I am unsure what strings you are targeting. I've made a Rubular with some examples:
http://rubular.com/r/0WijBsdtV0
Do these match your intended behavior?
I'm trying to find a substring that looks like "3/4" (any number 0-9 for the numerator and denominator). However, the text that I am trying to parse may contain only a "/4" if its 1/4. In those cases, I don't care about the "/" and only want the denominator. My current regular expression is "[0-9?\\/0-9]" but it returns '3/4' one by one, when I wanted it grouped instead.
Does anyone have a fix for this?
Thanks so much!
This regex:
[0-9?\/0-9]
is a character class that matches a single digit, question mark, or slash. You want this:
[0-9]?\/[0-9]
or this:
\d?\/\d
I think you want to use the pipe to create an or:
([0-9]\/[0-9])|\/([0-9])
The downside of this approach though would be that you'd need to validate if group 1 or 2 returned a match... so if you had a full fraction you'd get a result in the first group, if you matched a denominator, the denominator would show up in the second group.
Might make more sense to:
([0-9]*\/[0-9])
And then check the length of each match back, and strip out the '/' if you have to.
use a capture group via the ( ) operator
[0-9]?\/([0-9])
Try this instead:
"([0-9]*\\?[0-9]+)"