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!
^.*?(?:g31|g32)(?!.*?(?:g21|g22)).*?(?:g11|g12).*$
Try this.See demo:
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다