Python regex - why does end of string ($ and \Z) not work with group expressions?

前端 未结 3 1202
余生分开走
余生分开走 2020-12-09 04:48

In Python 2.6. it seems that markers of the end of string $ and \\Z are not compatible with group expressions. Fo example

import re         


        
3条回答
  •  鱼传尺愫
    2020-12-09 05:11

    A [..] expression is a character group, meaning it'll match any one character contained therein. You are thus matching a literal $ character. A character group always applies to one input character, and thus can never contain an anchor.

    If you wanted to match either a whitespace character or the end of the string, use a non-capturing group instead, combined with the | or selector:

    r"\w+(?:\s|$)"
    

    Alternatively, look at the \b word boundary anchor. It'll match anywhere a \w group start or ends (so it anchors to points in the text where a \w character is preceded or followed by a \W character, or is at the start or end of the string).

提交回复
热议问题