python 3.x - Regex: match "group3.*group1" if group2 is not between groups 3 and 1 -
i'm using python 3.4. have 3 groups:
g1 = 'g11|g12' g2 = 'g21|g22' g3 = 'g31|g32'
i want match instance of g3, followed except g2, followed g1. here strings on want find match:
'g31 or nothing g11' 'g31g11' edit: added: 'anything or nothing g31 g11 or nothing' edit: added: 'anything or nothing g21 g31 g11 or nothing'
and here strings on not want find match:
'g31 or nothing g21 or nothing g11' 'g31g21g11'
i tried: (g31|g32)(?=.*?(g11|g12))(?!.*?(g21|g22))
, works 'g31 g11'
, 'g31 g21 g11'
fails if there g21 or g22 after g11, in 'g31 g11 g21'
.
i've tried '(g31|g32).*?(g21|g22){0}.*?(g11|g22)'
works 'g31 g11'
, 'g31 g21 g11'
not 'g31 g31 g21 g11'
.
the problem trivial if expect fixed width strings, e.g., 'g31 g11'
or 'g31 g21 g11'
, there many solutions such problem on stackoverflow already. also, have presented problem without groups want avoid solutions use []
.
i hope won't told isn't possible regex if is, it.
thank you!
^.*?(?:g31|g32)(?!.*?(?:g21|g22)).*?(?:g11|g12).*$
try this.see demo:
Comments
Post a Comment