regex - How to match (group2.*|^.*)group1 when no instance of groups 1,2,3, or 4 are in between? -
i'm using python 3.4.
suppose have 4 groups composed of regular expressions
g1 = 'g11|g22|...|g1m' g2 = 'g21|g22|...|g2n' g3 = 'g32|g32|...|g3p' g4 = 'g41|g42|...|g4q'
for example, g1
might 'chickens|horses|bonnet(?>!blue )'
. groups disjoint: no element in of 4 groups belongs more 1 group. groups can have number of elements greater 1.
i want match on string if , if contains any instance of group_1 such either :
- no instances of of groups 1-4 precede said instance of group_1 or
- the instance of of groups 1-4 precedes said instance of group_1 not group_2.
some strings on want match:
'g11'
'g31 g11'
'g41g11'
'g11 g21 g11'
(the second instance of g11 violates rule 2. first instance of g11 not , rule 1 satisfied.)'anything or nothing g11 or nothing'
'anything or nothing g31 or nothing g11'
some strings on don't want match:
'g31 g21 g11'
'g21 g11 g31'
'anything or nothing g21 or nothing g11 or nothing'
what've tried far:
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'
.
^(?!(?:(?!g1|g2).)*(?:g21|g22)(?:(?!g31|g32|g41|g42).)*(?:g11|g12)).*?(?:g11|g12).*$
you can try this.see demo.
Comments
Post a Comment