Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to match groups with no specific order

Tags:

java

regex

I have 4 complex regex patterns, A, B, C and D. I need to locate all the patters that are of the format of A(B AND C AND D) where the order of B,C,D does not matter and C and D are optional. Is there a way to do such a thing in regex without writing all the possible permutations of B,C,D with or (|) between them?

I'm programming this in Java, and prefer to performance-friendly. Thanks!

Edit: Changing 3 complex patters to 4 complex regex patterns.

like image 748
apines Avatar asked Feb 17 '12 09:02

apines


People also ask

What does ?= Mean in regex?

?= is a positive lookahead, a type of zero-width assertion. What it's saying is that the captured match must be followed by whatever is within the parentheses but that part isn't captured. Your example means the match needs to be followed by zero or more characters and then a digit (but again that part isn't captured).

What does regex 0 * 1 * 0 * 1 * Mean?

Basically (0+1)* mathes any sequence of ones and zeroes. So, in your example (0+1)*1(0+1)* should match any sequence that has 1. It would not match 000 , but it would match 010 , 1 , 111 etc. (0+1) means 0 OR 1.

Does order matter in regex?

The order of the characters inside a character class does not matter. The results are identical. You can use a hyphen inside a character class to specify a range of characters. [0-9] matches a single digit between 0 and 9.

What is non capturing group in regex?

tl;dr non-capturing groups, as the name suggests are the parts of the regex that you do not want to be included in the match and ?: is a way to define a group as being non-capturing. Let's say you have an email address [email protected] . The following regex will create two groups, the id part and @example.com part.


1 Answers

No. You have to write all the permutations. It is a limitation of the regular languages. Once you do it however, it will be as performance friendly as any other regular expression.

like image 155
Johannes Avatar answered Sep 27 '22 23:09

Johannes