Python Regular Expression Operations – Regex


The regular expression is a popular topic in system administrators and developers. A regular expression is used to find structured text or string in single or multiple files. The best side of regular expression we can define whatever we want to match string in texts. Python supports and provides a lot of methods for regular expressions and related operations. In this tutorial, we will look at these regex functions in detail.

Import Re or Regular Expression Library

In order to work with regular expressions in python, we need to import regular expression library which is named as a shortcut of regular expression as regex .

import regex

Match

The match function is one of the most popular functions which will apply regex pattern into the given string. We will use match function with pattern and string parameters. There is also flags parameter which can be used to provide some flags like the case, interpretation, etc. If we do not provide flags there will be no error.

re.match(PATTERN,STRING,FLAG)

In this example, we want to find words that are delimited by spaces in the given string. Each word provides single match and those matches will be grouped.

line="This is an example about regular expression"

matches = re.match('\w+',line)

matches.group(0)

Groups

In the previous part, we have simply printed the first group which index is   but we may have more than one word to match in a line. It is called a group in the regex. We can match multiple different patterns in a single match.

In this example we will match words starts with T and a into two groups.

line="This is an example about regular expression" 
matches = re.match('(T\w+).*example\s(a\w+)',line) 
matches.group(0) 
#'This is an example about' 
matches.group(1) 
#'This' 
matches.group(2) 
#'about'
Groups
Groups

As we see matched pattern results are assigned into groups. We can get them by providing an index about these groups.

Search

Search is similar to the match function but the main difference is match looks up to the first match and then stops but the search will look at to the end of the string and will find multiple matches if exists. The syntax of the search function is the same match functions.

LEARN MORE  PHP Glob() Function To Match Path, Directory, File Names with Examples

re.search(PATTERN,STRING,FLAG)

line="This is an example about regular expression" 
matches = re.search('(T\w+).*example\s(a\w+)',line) 
matches.group(0) 
#'This is an example about' 
matches.group(1) 
#'This' 
matches.group(2) 
#'about'

Search and Replace

Python regex functions support finding given text and replacing the text with a new one. We will use sub functions in order to replace. sub function supports the following syntax.

re.sub(PATTERN,NEWTEXT,STRING,FLAG)

We will change regular word with unregular word in this example.

line="This is an example about regular expression" 
matches = re.sub('regular','unregular',line) 
print(matches)
Search and Replace
Search and Replace

Option Flags

Options flags generally provided as last parameter to the related regex functions. Option flags generally used to case-insensitive match, interpret with current locale etc. Here is a list of option flags.

  • re.I is used case-insensitive match
  • re.L is used for current locale
  • re.M makes $ match end of line
  • re.S makes . match any character, including newline

Case Insensitive

We can use option flags in order to make case-insensitive match or search with regular expression. We will provide re.I as last arguments to the relevant function like below.

matches = re.sub('regular','unregular',line,re.I)

Leave a Comment