-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,471
  • 17
  • 87
  • 173
user8270077
  • 4,213
  • 10
  • 55
  • 117
  • 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
  • 40,237
  • 31
  • 123
  • 190