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')
('', '')
ON
Any
ON
Any
Having read many articles on the internet and S.O. I think I still don't understand the regex word boundary: \b
The first regex doesn't give me the expected result I think it's must give me "ON Any On Any" but it still not give me that.
The second regex gives me tuples and I don't know why or understand the meaning of: ('', '')
The third regex gives prints the results on separated lines and empty lines in betweens
Could you please help me to understand that.
\b
is helpful. Surely it is not a universal remedy, andre.UNICODE
is sometimes necessary, or even(?<!\S)
/(?!\S)
lookarounds can turn out better alternatives, but the main point is that it does not move the regex index. If you have a space, match it. – Kelci