-1

What I am missing here?

import re
sample = 'this is an example'
p = re.compile('this is\b\w+\bexample')
p.findall(sample)
[]

Shouldn't the pattern match? \b\w+\b should match space + an + space or not?

halfer
  • 19,824
  • 17
  • 99
  • 186
user8270077
  • 4,621
  • 17
  • 75
  • 140
  • 1
    Read more about word boundaries, they are *non-consuming* patterns, zero-width assertions that do not actually match any chars, but *positions*. – Wiktor Stribiżew Apr 30 '20 at 17:49
  • **Duplicate of [How does \b work when using regular expressions?](https://stackoverflow.com/questions/7605198/how-does-b-work-when-using-regular-expressions)** – Wiktor Stribiżew Apr 30 '20 at 18:22

1 Answers1

0

Because neither \b, nor \w matches spaces.

import re
sample = "this is an example"
r = re.findall(r"this is\s+\w+\s+example", sample)
print(r)

See this demo.

Ωmega
  • 42,614
  • 34
  • 134
  • 203