简体   繁体   中英

How to extract multiline text using regex python

Hi I have the following text.

x = """Hello, this is a\\nmultiline text\\nend.Hello, this is\\nthe second chunck\\nend."""

This pattern of Hello, \\nend. keeps on repeating. I want to extract the text between each set of these two words. I tried using this

b=re.search(r'(?<=Hello,).+(?=end)', x, re.DOTALL)

but I get all the text from the start to the end. How do I get the separate chunks of text?

Thanks.p

Use a lazy quantifier : .+? instead of .+ .

The problem is that the .+ matches as far as it can, so just eats all the way to the end of the documents. Adding the question mark tells it to match as little as it can.

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