0

I want to match M.S. as word in text with:

re.search(r"\b(M.S.)\b", "M.S. in computer science.")
re.search(r"\b(M\.S\.)\b", "M.S. in computer science.")

But none of the above return any results.

Meysam
  • 105
  • 1
  • 1
  • 6
  • The last word boundary does not match. – The fourth bird Nov 09 '21 at 10:37
  • `\bM\.S\.\b` will match in `M.S.in computer science.` and not in `M.S. in computer science.` as `\b` means [this](https://stackoverflow.com/a/1324784/3832970). – Wiktor Stribiżew Nov 09 '21 at 10:39
  • "word matching with boundary not works" is not quite correct as the pattern works, just you did not expect it to work like that, and the reason is that you got confused with the word boundary construct. Please check the post I linked to, I have added a more versatile solution, adaptive word boundaries, to my answer. – Wiktor Stribiżew Nov 09 '21 at 11:04

0 Answers0