0

I'm studying regular expression with python and I'm trying the \b notation and tried below code, my understanding is that if a word starts with "the", pattern "\bthe" should match it, but somehow it doesn't match my "text" nor "text2" string, could somebody help take a look and explain? Thanks!

In the documentation it is said "Note that formally, \b is defined as the boundary between a \w and a \W character (or vice versa), or between \w and the beginning/end of the string. This means that r'\bfoo\b' matches 'foo', 'foo.', '(foo)', 'bar foo baz' but not 'foobar' or 'foo3'".


my code:

text="the"
text2="thetree"

patn='\bthe'

m=re.search(patn, text)
print(m)

m=re.search(patn, text2)
print(m)

Output: None None

1 Answers1

0

Always use raw strings (prefixed with r) for regex patterns. Without it, \b is interpreted as a string escape meaning "ASCII backspace". patn = r'\bthe' would do what you expect.

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271