Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex with ? for a set of words

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.

like image 690
Sonali Gupta Avatar asked May 02 '21 10:05

Sonali Gupta


People also ask

How do you find multiple words in a regular expression?

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.

What does '$' mean in regex?

$ 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.

What is the difference between () and [] in regex?

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.

What is \b in python regex?

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.


1 Answers

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 string

Regex 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
like image 63
The fourth bird Avatar answered Sep 28 '22 07:09

The fourth bird