REGEX - Differences between `^`, `$` and `\A`, `\Z`

前端 未结 1 2063
难免孤独
难免孤独 2020-12-15 09:29

As I know, re proposes the following boundary matches.

  • ^ matches at the beginning of a line.
  • $ matches at the
相关标签:
1条回答
  • 2020-12-15 09:58

    The difference only becomes apparent when you use the re.M or re.MULTILINE multiline flag:

    >>> re.search(r'^word', 'Line one\nword on line two\n', flags=re.M)
    <_sre.SRE_Match object at 0x10124f578>
    >>> re.search(r'\Aword', 'Line one\nword on line two\n', flags=re.M) is None
    True
    

    where ^ matched at the start of a line (following a newline). $ matches at the end of a line:

    >>> re.search(r'word$', 'Line one word\nLine two\n', flags=re.M)
    <_sre.SRE_Match object at 0x10123e1d0>
    >>> re.search(r'word\Z', 'Line one word\nLine two\n', flags=re.M) is None
    True
    

    From the documentation:

    re.M
    re.MULTILINE

    When specified, the pattern character '^' matches at the beginning of the string and at the beginning of each line (immediately following each newline); and the pattern character '$' matches at the end of the string and at the end of each line (immediately preceding each newline). By default, '^' matches only at the beginning of the string, and '$' only at the end of the string and immediately before the newline (if any) at the end of the string.

    \A always matches at the start of the string regardless, \Z always at the end.

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