Clad
Clad

Reputation: 1561

Regular Expression to test the presence and match a string at the same time

I would like to determine whether a string S has a substring MYSUBSTRING preceded by two consecutive digits I need to determine.

For example:

'aaa79bbbMYSUBSTRINGccc' ==> I want 7, 9 and True (or 7, 9 and MYSUBSTRING)

'aaa79bbbccc' ==> I want 7, 9 and False (or 7, 9 and None)

Can I do that with a SINGLE regex? If so, which one?

Upvotes: 0

Views: 1660

Answers (3)

David Z
David Z

Reputation: 131550

Sure, you can use (\d)(\d).*?(MYSUBSTRING)?. In Python, you would use this in the re.search function like so:

s = ... # your string
m = re.search(r'(\d)(\d).*?(MYSUBSTRING)?', s)
m.group(1) # first digit
m.group(2) # second digit
m.group(3) # the substring, or None if it didn't match

Upvotes: 0

Andrew Clark
Andrew Clark

Reputation: 208435

The following regex should do it:

(\d)(\d)(?:.*?(MYSUBSTRING))?

>>> re.search(r'(\d)(\d)(?:.*?(MYSUBSTRING))?', 'aaa79bbbMYSUBSTRINGccc').groups()
('7', '9', 'MYSUBSTRING')
>>> re.search(r'(\d)(\d)(?:.*?(MYSUBSTRING))?', 'aaa79bbbccc').groups()
('7', '9', None)

Upvotes: 4

Jacob
Jacob

Reputation: 1536

A fun problem. This monstrosity:

(\d)(\d)(.(?!(MYSUBSTRING)))*.?(MYSUBSTRING)?

Seems to work for me.

Broken down:

(\d)(\d)              # capture 2 digits
(.(?!(MYSUBSTRING)))* # any characters not preceded by MYSUBSTRING
.?                    # the character immediately before MYSUBSTRINg
(MYSUBSTRING)?        # MYSUBSTRING, if it exists

Upvotes: 0

Related Questions