简体   繁体   中英

How to match carriage return with no linefeed in python

I am trying to use regular expressions in python to find carriage returns \\r in a string which have no following linefeed \\n, thus being a likely error.

However, my regex allways matches, and I do not know why:

>>> import re
>>> cr = re.compile("\r[^\n]", re.X)
>>> rr= rege.search("bla")
>>> rr
<_sre.SRE_Match object at 0x0000000003793AC0>

How would the correct syntax look like?

You can use a negative lookahead :

\r(?!\n)

In Python :

import re
rx = r'\r(?!\n)'
for match in re.finditer(rx, string):
    # do sth. here

You're using verbose mode ( re.X ) and a non-raw string. That means your regex has a literal carriage return and line feed in it. As these are whitespace characters and you're in verbose mode, the carriage return outside a character class is completely ignored. Your regex is effectively r'[^\\n]' , matching any non-line feed character.

Use a raw string. As others suggested, a negative lookahead would also be better for a "not followed by" assertion:

r'\r(?!\n)'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM