Regex: don't match string ending with newline (\n) with end-of-line anchor ($)

前端 未结 3 856
猫巷女王i
猫巷女王i 2021-01-05 13:00

I can\'t figure out how to match a string but not if it has a trailing newline character (\\n), which seems automatically stripped:

import re

p         


        
3条回答
  •  失恋的感觉
    2021-01-05 13:42

    The documentation says this about the $ character:

    Matches the end of the string or just before the newline at the end of the string, and in MULTILINE mode also matches before a newline.

    So, without the MULTILINE option, it matches exactly the first two strings you tried: 'foobar' and 'foobar\n', but not 'foobar\n\n', because that is not a newline at the end of the string.

    On the other hand, if you choose MULTILINE option, it will match the end of any line:

    >>> re.match(r'^foobar$', 'foobar\n\n', re.MULTILINE)
    <_sre.SRE_Match object; span=(0, 6), match='foobar'>
    

    Of course, this will also match in the following case, which may or may not be what you want:

    >>> re.match(r'^foobar$', 'foobar\nanother line\n', re.MULTILINE)
    <_sre.SRE_Match object; span=(0, 6), match='foobar'>
    

    In order to NOT match the ending newline, use the negative lookahead as DeepSpace wrote.

提交回复
热议问题