How to match a paragraph using regex

前端 未结 5 2047
误落风尘
误落风尘 2020-12-14 13:11

I have been struggling with python regex for a while trying to match paragraphs within a text, but I haven\'t been successful. I need to obtain the start and end positions o

5条回答
  •  别那么骄傲
    2020-12-14 13:29

    What is the newline symbol? Let us suppose the newline symbol is '\r\n', if you want to match the paragraphs starting with Lorem, you can do like this:

    pattern = re.compile('\r\nLorem.*\r\n')
    str = '...'    # your source text
    matchlist = re.findall(pattern, str)
    

    The matchlist will contain all the paragragh start with Lorem. And the other two words are the same.

提交回复
热议问题