This is probably a very simple question which has already been answered but can't find the solution online.
I'm trying the following code to search for the word foo
which works:
import re
haystack = 'bar foo, foo is a foobar'
needle = 'foo'
[m.start() for m in re.finditer(r'\b{}\b'.format(re.escape(needle)), haystack)]
# [4, 9]
while the following, searching for foo,
(note the comma) doesn't give what I would expect:
import re
haystack = 'bar foo, foo is a foobar'
needle = 'foo,'
[m.start() for m in re.finditer(r'\b{}\b'.format(re.escape(needle)), haystack)]
# []
what's going on and how can I search for such strings with special characters (in general, not the specific case reported above)?
Thanks