Regex: match "group3.*group1" if group2 is not between groups 3 and 1

108 views Asked by At

I'm using Python 3.4. I have three groups:

g1 = 'g11|g12'
g2 = 'g21|g22'
g3 = 'g31|g32'

I want to match any instance of g3, followed by anything except g2, followed by g1. Here are some strings on which I want to find a match:

'g31 anything or nothing g11'  
'g31g11'
EDIT:  ADDED:  'anything or nothing g31 g11 anything or nothing'
EDIT:  ADDED:  'anything or nothing g21 g31 g11 anything or nothing'

and here are some strings on which I do not want to find a match:

'g31 anything or nothing g21 anything or nothing g11'
'g31g21g11'

I tried: (g31|g32)(?=.*?(g11|g12))(?!.*?(g21|g22)), which works for 'g31 g11' and 'g31 g21 g11' but fails if there is a g21 or g22 after g11, as in 'g31 g11 g21'.

I've also tried '(g31|g32).*?(g21|g22){0}.*?(g11|g22)' which works for 'g31 g11' and 'g31 g21 g11' but not 'g31 g31 g21 g11'.

The problem would be trivial if I could expect fixed width strings, e.g., 'g31 g11' or 'g31 g21 g11' and there are many solutions to such a problem on stackoverflow already. Also, I could have presented this problem without groups but I want to avoid any solutions that use [].

I hope that I won't be told that this isn't possible with regex but if it is, so be it.

Thank you!

2

There are 2 answers

6
vks On BEST ANSWER
^.*?(?:g31|g32)(?!.*?(?:g21|g22)).*?(?:g11|g12).*$

Try this.See demo:

https://regex101.com/r/hI0qP0/9#python

5
karthik manchala On

It is entirely possible.. You can use the following regex:

 g3\d+(?:(?!g2).)*g1\d+

See DEMO