Quantcast
Channel: Regex Match all characters between two strings - Stack Overflow
Viewing all articles
Browse latest Browse all 19

Answer by MCheng for Regex Match all characters between two strings

$
0
0

For python

def match_between_strings(text, start_str, end_str):    pattern = re.escape(start_str) + r'(.*?)'+ re.escape(end_str)    matches = re.findall(pattern, text, re.DOTALL)    return matches

Example usage:

start_str = "This"end_str = "sentence"text = "This is just\na simple sentence"result = match_between_strings(text, start_str, end_str)

Result

[' is just\na simple ']


Viewing all articles
Browse latest Browse all 19

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>