Home >Backend Development >Python Tutorial >How to Extract Text Between Strings with Regular Expressions in Python?
Extracting Text Between Strings Using Regular Expressions
In Python, you can leverage regular expressions to extract text located between two specified strings within a larger string. Consider the following example:
"Part 1. Part 2. Part 3 then more text"
Your objective is to isolate the text between "Part 1" and "Part 3," which is ". Part 2. ". To achieve this, you can employ the re.search() function:
<code class="python">import re s = 'Part 1. Part 2. Part 3 then more text' match = re.search(r'Part 1\.(.*?)Part 3', s) if match: text_between = match.group(1) print(text_between)</code>
In this case, the regular expression r'Part 1.(.*?)Part 3' assigns ".*?" as a capture group. The "?" ensures that this group is non-greedy, meaning it will capture the shortest possible string that satisfies the regular expression. The .* matches any character, and the . represents any character except a newline.
If multiple occurrences exist, you can use re.findall() instead:
<code class="python">matches = re.findall(r'Part 1(.*?)Part 3', s) for match in matches: print(match)</code>
The above is the detailed content of How to Extract Text Between Strings with Regular Expressions in Python?. For more information, please follow other related articles on the PHP Chinese website!