Word boundary with regex - cannot extract all words

耗尽温柔 提交于 2019-11-29 16:03:21

Use lookarounds to extract words inside whitespace boundaries:

r'(?<!\S)Male-Cat(?!\S)'

See the online regex demo

Details

  • (?<!\S) - a whitespace or start of string must appear immediately to the left of the current location
  • Male-Cat - the term to search for
  • (?!\S) - a whitespace or end of string must appear immediately to the right of the current location

Since (?<!\S) and (?!\S) are zero-width assertions, the whitespace won't be consumed, and consecutive matches will get found.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!