import re sstring = "ON Any ON Any" regex1 = re.compile(r''' \bON\bANY\b''', re.VERBOSE) regex2 = re.compile(r'''\b(ON)?\b(Any)?''', re.VERBOSE) regex3 = re.compile(r'''\b(?:ON)?\b(?:Any)?''', re.VERBOSE) for a in regex1.findall(sstring): print(a) print("----------") for a in regex2.findall(sstring): print(a) print("----------") for a in regex3.findall(sstring): print(a) print("----------")
('ON', '') ('', '') ('', 'Any') ('', '') ('ON', '') ('', '') ('', ' Any ')
('', '')
ABOUT
Any
ABOUT
Any
After reading many articles on the Internet and SO, I think I still do not understand the boundary of the regular expression word: \b
The first regular expression does not give the expected result, I think it should give me "ON Any On Any", but it still does not give me this.
The second regular expression gives me tuples, and I don’t know why or understand the meaning: ('', '')
Third regexp displays results on highlighted lines and empty lines in betweens
Could you help me figure this out.
python regex word-boundary
Ahmad Kamal ELSaman
source share