In this article we want to learn about Python Regex Libraries, so regular expressions are commonly known as regex, it is powerful tool for working on text. they are used to search, extract and replace specific patterns inside the text. Python provides several regex libraries that can be used to work with text data. in this article we want to talk about some of the most popular Python regex libraries and their features.
-
re library
re library is builtin Python library and it provides support for regular expressions. also it allows users to search, extract and replace specific patterns inside a string. re module provides several functions, including search(), findall(), sub() and split() and you can use these functions to manipulate text data.
This is an example of how to use re library to extract phone numbers from a string:
1 2 3 4 5 |
import re text = "My phone number is 123-456-7890" phone_number = re.search(r'\d{3}-\d{3}-\d{4}', text).group() print(phone_number) |
Above code will extract the phone number from string using the search() function and the regular expression pattern \d{3}-\d{3}-\d{4}.
This will be the result

-
regex library
regex library is third party Python library that provides advanced regular expression features compared to the builtin re library. because it supports Unicode character properties, named capture groups and lookarounds.
This is an example of how to use the regex library to extract all email addresses from a string:
1 2 3 4 5 |
import regex text = "My email is myemail@geekscoders.com and my friend email is friend@geekscoders.com.com" email_addresses = regex.findall(r'\b\w+@\w+\.\w+\b', text) print(email_addresses) |
Above code will extract all email addresses from string using the findall() function and regular expression pattern \b\w+@\w+\.\w+\b.
This will be the result

-
pyre2 library
pyre2 library is another third party Python library and it provides advanced regular expression features. it is based on the RE2 library, it is highly optimized regular expression engine that supports very large regular expressions and large amounts of text data, make sure that you have already installed this library, pip install pyre2.
1 2 3 4 5 |
import pyre2 text = "Visit my website at https://www.geekscoders.com" urls = pyre2.findall(r'https?://(?:[-\w.]|(?:%[\da-fA-F]{2}))+', text) print(urls) |
Above code will extract all URLs from string using findall() function and the regular expression pattern https?://(?:[-\w.]|(?:%[\da-fA-F]{2}))+.