1

input :

- "example (.com)"

output :

- "example"

What I tried

import re
pattern=re.compile(r'\b\([\W\w]+\)\b')
#pattern=re.compile(r'\([\W\w]+\)')
print(pattern.sub("","example (.com)"))

This doesn't work but if I remove \b it works fine - why?

Jan
  • 42,290
  • 8
  • 54
  • 79
sakeesh
  • 919
  • 1
  • 10
  • 24
  • There's no word boundary there. – user2357112 Apr 05 '20 at 06:56
  • This is confusing I thought the Opening braces is the boundary start and closing braces is the boundary end , why is it that you say there are no word boundary ? – sakeesh Apr 05 '20 at 07:24
  • There is no [word boundary](https://www.regular-expressions.info/wordboundaries.html) between a space and `(` Also see https://stackoverflow.com/questions/1324676/what-is-a-word-boundary-in-regex – The fourth bird Apr 05 '20 at 07:25

2 Answers2

0

Make yourself clear what the \b stands for:

(?:(?=\w)(?<!\w)|(?<=\w)(?!\w))

That is, either a word character ahead and not behind or the other way round. A word character (\w) stands for [A-Za-z0-9_] and does not include ( or ), so there's no word boundary between a space and a parenthesis.

Jan
  • 42,290
  • 8
  • 54
  • 79
-1

( is not a word boundary, so \b won't match it. Instead you could use \B to match the blank

bn_ln
  • 1,648
  • 1
  • 6
  • 13
  • `\B` does not match blanks, and even if it did, that would be the wrong thing to do here. `\B` matches any position that is not a word boundary. – user2357112 Apr 05 '20 at 07:32
  • oops, sorry I was wrong to write that \B matches the blank, but I do see it working in this case, why do you suggest it's wrong in this instance? – bn_ln Apr 05 '20 at 07:55