Regex: match "group3.*group1" if group2 is not between groups 3 and 1

epistemicFUN

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!

vks
^.*?(?:g31|g32)(?!.*?(?:g21|g22)).*?(?:g11|g12).*$

Try this.See demo:

https://regex101.com/r/hI0qP0/9#python

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

How to match regex single combine with multi groups same pattern in Perl

분류에서Dev

JavaScript : RegEx-match1 match2 등을 반환하지 않음

분류에서Dev

Regex Match Any Character Between (Inclusive) {}

분류에서Dev

grep: regex only for match anything between parenthesis

분류에서Dev

group1, group2, overlap_count에서 중복 행렬을 만드시겠습니까?

분류에서Dev

Regex to match words separated by '!' and enclosed between curly brackets '{' '}'

분류에서Dev

Java Regex match anything that matches given pattern and is not between a given character

분류에서Dev

regex - How to match elements while ignoring others between quotation marks?

분류에서Dev

nim re, regex modul is not filling match group

분류에서Dev

Regex 2 in 1 for comments

분류에서Dev

Regex replace multiple groups with wildcards

분류에서Dev

Regex to match "a string of length less than X resides between two ">" symbols"

분류에서Dev

Match File1 with File 2

분류에서Dev

regex (?!) and (?=) and null match

분류에서Dev

Regex multiline match

분류에서Dev

Match regex with \\n in it

분류에서Dev

Regex match tag with no spaces

분류에서Dev

Regex match for split

분류에서Dev

Regex to match URL in Powershell

분류에서Dev

Regex to match end of the word

분류에서Dev

.match() problems with regex

분류에서Dev

add apostrophe to regex match

분류에서Dev

RegEx to match list

분류에서Dev

Regex match in jQuery

분류에서Dev

PHP, regex preg_match 특정 단어 0 또는 1 회

분류에서Dev

Regex returns groups when testing but not in code?

분류에서Dev

Create regex from variable with capture groups in javascript

분류에서Dev

Regex match everything after conditional match

분류에서Dev

Python Regex match: include Regex syntax

Related 관련 기사

뜨겁다태그

보관