How to match (group2. * | ^. *) Group1 when there is no instance of groups 1,2,3 or 4?
I am using Python 3.4.
Suppose we have four groups of regex
g1 = 'g11|g22|...|g1m'
g2 = 'g21|g22|...|g2n'
g3 = 'g32|g32|...|g3p'
g4 = 'g41|g42|...|g4q'
For example, g1
maybe 'chickens|horses|bonnet(?>!blue )'
. Groups do not overlap: none of the four groups belongs to more than one group. Groups can have any number of elements, greater than 1.
I want to match a string if and only if contains any instance of group_1 such as:
- no instances of any group 1-4 precede the specified instance of group_1, or
- an instance of any of groups 1-4 immediately preceding the specified instance of group_1 is not group_2.
Some lines I want to match on:
-
'g11'
-
'g31 g11'
-
'g41g11'
-
'g11 g21 g11'
(The second instance of g11 violates rule 2. The first instance of g11 is not executed and, in addition, rule 1 is satisfied). -
'anything or nothing g11 anything or nothing'
-
'anything or nothing g31 anything or nothing g11'
Some lines on which I don't want to match:
-
'g31 g21 g11'
-
'g21 g11 g31'
-
'anything or nothing g21 anything or nothing g11 anything or nothing'
What have you tried:
-
I've tried:,
(g31|g32)(?=.*?(g11|g12))(?!.*?(g21|g22))
which works for'g31 g11'
and'g31 g21 g11'
, but fails if there is g21 or g22 after g11, as in'g31 g11 g21'
. -
I have also tried
'(g31|g32).*?(g21|g22){0}.*?(g11|g22)'
which works for'g31 g11'
and'g31 g21 g11'
but not'g31 g31 g21 g11'
.
source to share