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 different regex libraries that you can use for working 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 you can use that 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 768-346-2345" 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, and it provides advanced regular expression features compared to the builtin re library. because it supports Unicode character properties, named capture groups and lookarounds, make sure that you have installed this library ( pip install regex).
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 admin@codeloop.org and my friend email is newsletter@codeloop.org" email_addresses = regex.findall(r'\b\w+@\w+\.\w+\b', text) print(email_addresses) |
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://codeloop.org" 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}))+.
Subscribe and Get Free Video Courses & Articles in your Email