Extract text between two strings with Python regex

Python is very light and convenient for manipulating regex. To extract the text between two strings tag1 and tag2 from the string content, we can use the Python regex library. We only need 2 lines of code:

pattern = "(?<=tag1).*(?=tag2)"
extracted =, content).group(0)

re is the Python regex library, you will need to import it.

You can put everything into a runnable Python file and print extracted:

import re

content = "blablatag1extract me !tag2 blalalalala"
pattern = "(?<=tag1).*(?=tag2)"
extracted =, content).group(0)


When you execute the code above, you will get the following result in the console:

extract me !

Leave a Reply

Your email address will not be published. Required fields are marked *