In this Python RegEx tutorial, we will learn-

Regular Expression Syntax
Example of w+ and ^ Expression
Example of \s expression in re.split function
Using regular expression methods
Using re.match()
Finding Pattern in Text (re.search())
Using re.findall for text
Python Flags
Example of re.M or Multiline Flags

For instance, a Python regular expression could tell a program to search for specific text from the string and then to print out the result accordingly. Expression can include

Text matching Repetition Branching Pattern-composition etc.

Regular expression or RegEx in Python is denoted as RE (REs, regexes or regex pattern) are imported through re module. Python supports regular expression through libraries. RegEx in Python supports various things like Modifiers, Identifiers, and White space characters.

Regular Expression(RE) Syntax

“re” module included with Python primarily used for string searching and manipulation Also used frequently for web page “Scraping” (extract large amount of data from websites)

We will begin the expression tutorial with this simple exercise by using the expressions (w+) and (^).

Example of w+ and ^ Expression

“^”: This expression matches the start of a string “w+“: This expression matches the alphanumeric character in the string

Here we will see a Python RegEx Example of how we can use w+ and ^ expression in our code. We cover the function re.findall() in Python, later in this tutorial but for a while we simply focus on \w+ and ^ expression. For example, for our string “guru99, education is fun” if we execute the code with w+ and^, it will give the output “guru99”.

Remember, if you remove +sign from the w+, the output will change, and it will only give the first character of the first letter, i.e., [g]

Example of \s expression in re.split function

“s”: This expression is used for creating a space in the string

To understand how this RegEx in Python works, we begin with a simple Python RegEx Example of a split function. In the example, we have split each word using the “re.split” function and at the same time we have used expression \s that allows to parse each word in the string separately.

When you execute this code it will give you the output [‘we’, ‘are’, ‘splitting’, ‘the’, ‘words’]. Now, let see what happens if you remove “\” from s. There is no ‘s’ alphabet in the output, this is because we have removed ‘\’ from the string, and it evaluates “s” as a regular character and thus split the words wherever it finds “s” in the string.

Similarly, there are series of other Python regular expression that you can use in various ways in Python like \d,\D,$,.,\b, etc. Here is the complete code Next, we will going to see the types of methods that are used with regular expression in Python.

Using regular expression methods

The “re” package provides several methods to actually perform queries on an input string. We will see the methods of re in Python:

re.match() re.search() re.findall()

Note: Based on the regular expressions, Python offers two different primitive operations. The match method checks for a match only at the beginning of the string while search checks for a match anywhere in the string. For example, consider the following code of Python re.match() function. The expression “w+” and “\W” will match the words starting with letter ‘g’ and thereafter, anything which is not started with ‘g’ is not identified. To check match for each element in the list or string, we run the forloop in this Python re.match() Example.

In order to use search() function, you need to import Python re module first and then execute the code. The Python re.search() function takes the “pattern” and “text” to scan from our main string

For example here we look for two literal strings “Software testing” “guru99”, in a text string “Software Testing is fun”. For “software testing” we found the match hence it returns the output of Python re.search() Example as “found a match”, while for word “guru99” we could not found in string hence it returns the output as “No match”.

How to Use re.findall() in Python?

Here we have a list of e-mail addresses, and we want all the e-mail addresses to be fetched out from the list, we use the method re.findall() in Python. It will find all the e-mail addresses from the list.

Here is the complete code for Example of re.findall()

Python Flags

Many Python Regex Methods and Regex functions take an optional argument called Flags. This flags can modify the meaning of the given Python Regex pattern. To understand these we will see one or two example of these Flags. Various flags used in Python includes

Example of re.M or Multiline Flags

In multiline the pattern character [^] match the first character of the string and the beginning of each line (following immediately after the each newline). While expression small “w” is used to mark the space with characters. When you run the code the first variable “k1” only prints out the character ‘g’ for word guru99, while when you add multiline flag, it fetches out first characters of all the elements in the string.

Here is the code

We declared the variable xx for string ” guru99…. careerguru99….selenium” Run the code without using flags multiline, it gives the output only ‘g’ from the lines Run the code with flag “multiline”, when you print ‘k2’ it gives the output as ‘g’, ‘c’ and ‘s’ So, the difference we can see after and before adding multi-lines in above example.

Likewise, you can also use other Python flags like re.U (Unicode), re.L (Follow locale), re.X (Allow Comment), etc.

Python 2 Example

Above codes are Python 3 examples, If you want to run in Python 2 please consider following code.

Summary

A regular expression in a programming language is a special text string used for describing a search pattern. It includes digits and punctuation and all special characters like $#@!%, etc. Expression can include literal

Text matching Repetition Branching Pattern-composition etc.

In Python, a regular expression is denoted as RE (REs, regexes or regex pattern) are embedded through Python re module.

“re” module included with Python primarily used for string searching and manipulation Also used frequently for webpage “Scraping” (extract large amount of data from websites) Regular Expression Methods include re.match(),re.search()& re.findall() Other Python RegEx replace methods are sub() and subn() which are used to replace matching strings in re Python Flags Many Python Regex Methods and Regex functions take an optional argument called Flags This flags can modify the meaning of the given Regex pattern Various Python flags used in Regex Methods are re.M, re.I, re.S, etc.