Word boundary with regex - cannot extract all words

后端 未结 1 645
梦如初夏
梦如初夏 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'(?

    See the online regex demo

    Details

    • (? - 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 (? and (?!\S) are zero-width assertions, the whitespace won't be consumed, and consecutive matches will get found.

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