简体   繁体   中英

Regex matching the end of a line correctly

I'm looking for a way to re.sub lines ending with \\n and \\r\\n while preserving the line ending.

import re
data = "Foo\r\nFoo\nFoo"
regex = r"^Foo$"
re.sub(regex, "Bar", data, flags=re.MULTILINE)

leaves me with

Foo\r\nBar\nBar

When using the regex ^Foo(\\r)?$ to check for an optional \\r , I'll end up with

Bar\nBar\nBar

Any ideas?

Edit : Expected result

Bar\r\nBar\nBar

Make the \\r and \\n optional with ? , and specify them as a capture group so you can refer to them in the callback function for the replacement:

>>> re.sub('^Foo(\r?\n?)$', r'Bar\1', 'Foo\r\nFoo\nFoo', flags=re.MULTILINE)
'Bar\r\nBar\nBar'

This will match lines ending in \\r , \\n , or \\r\\n , and lines without a linefeed at all.

Use positive lookahead assertion

import re
data = "Foo\r\nFoo\nFoo"
regex = r"^Foo(?=\r?\n?$)"
re.sub(regex, "Bar", data, flags=re.MULTILINE)

Output :

'Bar\r\nBar\nBar'

Regex explanation here.

正则表达式可视化

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