Categories
Python Answers

How to extract part of a regex match with Python?

Spread the love

Sometimes, we want to extract part of a regex match with Python.

In this article, we’ll look at how to extract part of a regex match with Python.

How to extract part of a regex match with Python?

To extract part of a regex match with Python, we can use the re.search method with a regex that gets a match from within a pattern.

For instance, we write:

import re

html = '<title>hell world</title>'
title_search = re.search('<title>(.*)</title>', html, re.IGNORECASE)

if title_search:
    title = title_search.group(1)
print(title)

We want to extract the text between the title tags in html.

To do that, we call re.search with '<title>(.*)</title> to get the content between the title tags.

Then we pass in html and re.IGNORECASE as the other arguments to search html in a case-insensitive manner.

Then we get the match from the regex group with title_search.group(1).

Therefore, title should be 'hello world'.

Conclusion

To extract part of a regex match with Python, we can use the re.search method with a regex that gets a match from within a pattern.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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