Regex Match all characters between two strings

后端 未结 14 1221
星月不相逢
星月不相逢 2020-11-21 07:42

Example: \"This is just\\na simple sentence\".

I want to match every character between \"This is\" and \"sentence\". Line breaks should be ignored. I can\'t figure o

14条回答
  •  我在风中等你
    2020-11-21 08:00

    There is a way to deal with repeated instances of this split in a block of text? FOr instance: "This is just\na simple sentence. Here is some additional stuff. This is just\na simple sentence. And here is some more stuff. This is just\na simple sentence. ". to matches each instance instead of the entire string, use below code:

    data = "This is just\na simple sentence. Here is some additional stuff. This is just\na simple sentence. And here is some more stuff. This is just\na simple sentence."
    
    pattern = re.compile('This is (?s).*? sentence')
    
    for match_instance in re.finditer(pattern, data):
        do_something(match_instance.group())
    

提交回复
热议问题