简体   繁体   中英

Match multi-line using python in linux

I met one problem when I used python regex in Linux. The target string has multi-line such as

This is a matched string_1.
This is a matched string_22.

Do not match this line.

What I want to do is match everything before "\\n\\n". I used

deleteString = re.compile('[\s\S]+\n\n')

but it's seems doesn't work in Linux.

How can I match the string before double \\n.

Thank you for your reply.

You don't need a regex in this case:

import re
import sys

text = sys.stdin.read()

# using str.find()
result = text[:text.find('\n\n') + 1]

# using re
result2 = re.match(r'(.*?)$^$', text, flags=re.DOTALL | re.MULTILINE).group(1)

# check that the result is the same
for r in [result, result2]:
     print(repr(r))
assert result == result2

Output

'This is a matched string_1.\nThis is a matched string_22.\n'
'This is a matched string_1.\nThis is a matched string_22.\n'

If you're reading the input from a file in a text mode then Python automatically translates platform-specific newlines to '\\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