Word boundary with regex - cannot extract all words

后端 未结 1 642
梦如初夏
梦如初夏 2020-12-11 22:03

I need extract double Male-Cat:

a = \"Male-Cat Male-Cat Male-Cat-Female\"
b = re.findall(r\'(?:\\s|^)Male-Cat(?:\\s|$)\', a)
print (b)
[\'Male-C         


        
相关标签:
1条回答
  • 2020-12-11 22:35

    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.

    0 讨论(0)
提交回复
热议问题