3

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?

3 Answers 3

3

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']
Sign up to request clarification or add additional context in comments.

Comments

3

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

2 Comments

I think this complicated answer is a result of misunderstanding of the requirement.
@nhahtdh - True that. It did seem though he was trying to match both in one go but could not resolve the order of appearance.
1

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

1 Comment

This will match aaa aaa in aaa aaa bbb.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.