Python Regex Libraries

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.

 

 

  1. 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:

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

Python Regex Libraries
Python Regex Libraries

 

 

  1. 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:

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

Python Regex Libraries
Python Regex Libraries

 

 

 

  1. 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.

Above code will extract all URLs from string using findall() function and the regular expression pattern https?://(?:[-\w.]|(?:%[\da-fA-F]{2}))+.

 

 

Learn More

Leave a Comment