简体   繁体   中英

Python regex match two string at two different order?

I want to match aaa bbb and bbb aaa in the following string:

aaa  bbb   bbb    aaa

using

match = re.search("^(?=.*(aaa))(?=.*?(bbb)).*$", subject, re.DOTALL | re.IGNORECASE)

see https://www.regex101.com/r/vA0nB0/2

But it only match aaa bbb .

How can I match bbb aaa too?

If Python supports conditionals:

For out of order stuff, just use a conditional.

Note - change the quantifier in the group to reflect how many items
are required to match. Below shows {2} which requires both items somewhere.
You could change it to {1,} - at least 1, or + - same thing.

(?:.*?(?:((?(1)(?!))aaa.*?bbb)|((?(2)(?!))bbb.*?aaa))){2}

Formatted :

 (?:
      .*? 
      (?:
           (                        # (1)
                (?(1)
                     (?!)
                )
                aaa .*? bbb 
           )
        |  (                        # (2)
                (?(2)
                     (?!)
                )
                bbb .*? aaa 
           )
      )
 ){2}

Output:

 **  Grp 0 -  ( pos 0 , len 21 ) 
aaa  bbb   bbb    aaa
 **  Grp 1 -  ( pos 0 , len 8 ) 
aaa  bbb
 **  Grp 2 -  ( pos 11 , len 10 ) 
bbb    aaa

You could try the below simple regex.

>>> import re
>>> s = 'aaa  bbb   bbb    aaa'
>>> re.findall(r'aaa.*?bbb|bbb.*?aaa', s)
['aaa  bbb', 'bbb    aaa']

How about something like this, if you know that its always going to be aaa or bbb ?

>>> a = 'aaa  bbb   bbb    aaa'
>>> match = re.findall("(aaa|bbb).+?(aaa|bbb)", a, re.DOTALL | re.IGNORECASE)
>>> match
[('aaa', 'bbb'), ('bbb', 'aaa')]

Here is the regex101 link : https://www.regex101.com/r/qO3uD3/1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM