I need to match the following rules in long string:
- key1=.*(a1|a2).* OR key2=.*(b1|b2|b3).* OR key3=.*(c1|c2|c3).*
- AND key1=.*(d1|d2|d3|d4).*
the '=' is intended as 'contains any of'
I've tried the following regex on regex101.com, but is not working as expected:
.*((key1=(?=.*(a1|a2)))|(key2=(?=.*(b1|b2|b3)))|(key3=(?=.*(c1|c2|c3))))&(key1=(?=.*(d1|d2|d3|d4))).*
some string that should match:
key1=a2,d3
key2=b1,b3key1=d1
key2=b2key3=c3,a2key1=d4
key1=d2abckey2=b2,b3key1=a1
some string that should NOT match:
key1=d2
key1=a1key2=b1
key2=b2key3=a1
what is wrong with my regex expression? what do you think? many thank
You can use a single positive lookahead to make sure that key1 is present with at least an occurrence of d
and a digit 1-4.
Then you can use another lookahead to assert one of key 1, key2 or key3 with the allowed digits.
Note that you can shorten the alternations |
for (a1|a2)
to a character class a[12]
^(?=.*key1=[a-z0-9,]*d[1-4])(?=.*(?:key1=a[12]|key2=b[123]|key3=c[123])).+
The pattern matches:
^
Start of string(?=
Positive lookahead
.*key1=[a-z0-9,]*d[1-4]
Match key1= having a value of d1
d2
d3
d4
by optionally matching the allowed characters that precede it [a-z0-9,]*
)
Close lookahead(?=.*
Positive lookahead, assert what is at the right is
(?:
Non capture group to list the alternatives
key1=a[12]
Match key1=a1
or key1=a2
|
Orkey2=b[123]
Match key2 with the allowed values|
Orkey3=c[123]
Match key3 with the allowed values)
Close non capture group)
Close positive lookahead.+
Match 1 or more characters