I want to have a regex for NAME;NAME;NAME and also for NAME;NAME;NAME;NAME where the fourth occurrence of NAME is optional.
I have one regex as (.+);(.+);(.+) which matched the first pattern but not the second. I tried playing with ? but its not working out with (.+);(.+);(.+)(;(.+))? Basically, I want to achieve the fourth (.+) as zero or one occurence.
However, to recognize multiple words in any order using regex, I'd suggest the use of quantifier in regex: (\b(james|jack)\b. *){2,} . Unlike lookaround or mode modifier, this works in most regex flavours.
$ means "Match the end of the string" (the position after the last character in the string). Both are called anchors and ensure that the entire string is matched instead of just a substring.
In other words, square brackets match exactly one character. (a-z0-9) will match two characters, the first is one of abcdefghijklmnopqrstuvwxyz , the second is one of 0123456789 , just as if the parenthesis weren't there. The () will allow you to read exactly which characters were matched.
Inside a character range, \b represents the backspace character, for compatibility with Python's string literals. Matches the empty string, but only when it is not at the beginning or end of a word.
Using .+
matches 1+ times any character including ;
If you want to match 3 or 4 groups separated by a ; and not including it, you could use a negated character class [^;]+
with an optional group at the end of the pattern.
^([^;]+);([^;]+);([^;]+)(?:;([^;]+))?$
^
Start of string([^;]+);([^;]+);([^;]+)
Capture group 1, 2 and 3 matching any char except ;
(?:
Non capture group
;([^;]+)
Match ;
and capture any char except ;
in group 4)?
Close group and make it optional$
End of stringRegex demo
If the parts in between can not contain ;
you could also use split and count the number of the parts.
String arr[] = { "NAME;NAME;", "NAME;NAME;NAME", "NAME;NAME;NAME;NAME", "NAME;NAME;NAME;NAME;NAME" };
for (String s : arr) {
String [] parts = s.split(";");
if (parts.length == 3 || parts.length == 4) {
System.out.println(s);
}
}
Output
NAME;NAME;NAME
NAME;NAME;NAME;NAME
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With