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:
'aaa79bb开发者_如何学CbMYSUBSTRINGccc' ==> 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?
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)
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
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
精彩评论