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

前端 未结 3 1187
余生分开走
余生分开走 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).

    0 讨论(0)
  • 2020-12-09 05:12

    Square brackets don't indicate a group, they indicate a character set, which matches one character (any one of those in the brackets) As documented, "special characters lose their special meaning inside sets" (except where indicated otherwise as with classes like \s).

    If you want to match \s or end of string, use something like \s|$.

    0 讨论(0)
  • 2020-12-09 05:16

    Martijn Pieters' answer is correct. To elaborate a bit, if you use capturing groups

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

    you get:

    >>> re.findall("\w+(\s|$)", "green pears")
    [' ', '']
    

    That's because re.findall() returns the captured group (\s|$) values.

    Parentheses () are used for two purposes: character groups and captured groups. To disable captured groups but still act as character groups, use (?:...) syntax:

    >>> re.findall("\w+(?:\s|$)", "green pears")
    ['green ', 'pears']
    
    0 讨论(0)
提交回复
热议问题