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 :

  1. no instances of of groups 1-4 precede said instance of group_1 or
  2. the instance of of groups 1-4 precedes said instance of group_1 not group_2.

some strings on want match:

  1. 'g11'
  2. 'g31 g11'
  3. 'g41g11'
  4. 'g11 g21 g11' (the second instance of g11 violates rule 2. first instance of g11 not , rule 1 satisfied.)
  5. 'anything or nothing g11 or nothing'
  6. 'anything or nothing g31 or nothing g11'

some strings on don't want match:

  1. 'g31 g21 g11'
  2. 'g21 g11 g31'
  3. '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.

https://regex101.com/r/hi0qp0/16


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -