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

 

 

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

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, 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:

 

 

 

 

This will be the result

Python Regex Library
Python Regex Library

 

 

  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}))+.

 

Subscribe and Get Free Video Courses & Articles in your Email

 

Leave a Comment

Share via
Copy link
Powered by Social Snap
×