How To Use Regular Expression in python - python regex tutorial

Posted by Suraj Singh on January 11, 2017 · 23 mins read
hii readers,

                      Today,   we will discuss what is python regular expression? or where we can use regular expression? or How to use regular expression? Or What are the usages of re module? Etc.

if You are a Beginner in regular expression than this tutorial is Best Place for you!.

So, First, we will Start with Some Basic Query About Python regular Expression.

As Tutorial points Says :   
                "regular expression is a special sequence of characters that helps you match or find other strings or sets of strings, using a specialized syntax held in a pattern. Regular expressions are widely used in UNIX world. The module re provides full support for Perl-like regular expressions in Python. "

And For Complete Tutorial Series Click here

Python: An Introduction About re Module. 

As Python Documentations :

The remodule was added in Python 1.5 and provides Perl-style regular expression patterns. Earlier versions of Python came with the regexmodule, which provided Emacs-style patterns. The regexmodule was removed completely in Python 2.5.

Regular expressions (called REs, or regexes, or regex patterns) are essentially a tiny, highly specialized programming language embedded inside Python and made available through the remodule. Using this little language, you specify the rules for the set of possible strings that you want to match; this set might contain English sentences, or e-mail addresses, or TeX commands, or anything you like. You can then ask questions such as “Does this string match the pattern?”, or “Is there a match for the pattern anywhere in this string?”. You can also use REs to modify a string or to split it apart in various ways.

Regular expression patterns are compiled into a series of bytecodes which are then executed by a matching engine written in C. For advanced use, it may be necessary to pay careful attention to how the engine will execute a given RE, and write the RE in a certain way in order to produce bytecode that runs faster. Optimization isn’t covered in this document because it requires that you have a good understanding of the matching engine’s internals.

The regular expression language is relatively small and restricted, so not all possible string processing tasks can be done using regular expressions. There are also tasks that can be done with regular expressions, but the expressions turn out to be very complicated. In these cases, you may be better off writing Python code to do the processing; while Python code will be slower than an elaborate regular expression, it will also probably be more understandable 

And For Complete Tutorial Series Click here

                                      Useful Matching Characters For Python Regex 
Any Digit
Any Non-digit character
Any Character
Only a, b, or c
Not a, b, nor c
Characters a to z
Numbers 0 to 9
Any Alphanumeric character
Any Non-alphanumeric character
m Repetitions
m to n Repetitions
Zero or more repetitions
One or more repetitions
Optional character
Any Whitespace
Any Non-whitespace character
Starts and ends
Capture Group
Capture Sub-group
Capture all
Matches abc or def

Now, We Know Basic Things About Regular Expression.

So, Let’s Take a look on This Small Example.

Here, we will use Pattern Matching Using search.

import re
text = "the man of steel can break that wall."
strings = ['the', 'steel']
for string in strings:
match =, text)
if match:
print('Your Match Found : "{}" in "{}"'.format(string, text))
text_pos = match.span()
print('Did not find Any Match "{}"'.format(string))

For this example, 

we import the re module and create a simple string. 

Then we create a list of two strings that we’ll search for in the main string. 

Next, we loop over the strings 

we plan to search for and actually run a search for them. 

If there’s a match, we print it out. 

Otherwise, we tell the user that the string was not found.

There are a couple of other functions worth explaining in this example. 

You will notice that we call span

This gives us the beginning and ending positions of the string that matched. 

If you print out the text_pos that we assigned the span to, 

you’ll get a tuple like this: (14, 20).


you can just call some match methods, 

which is what we do next. 

We use to start and end to grab the starting and ending position of the match, 

which should also be the two numbers that we get from the span.

For More Complete Tutorial or For More Info Visit here:

And For Complete Tutorial Series Click here

Reference Link:

For Beginner Here I Created A Small Python Script.

In This Screenshot 

I Copied Source Code of

in Input Data Panel.


I Entered regex pattern on search To filter all links of blog pages.

after clicking search button.

I got Filtered Data in regex panel. (pretty simple)

Screen Shot of That Script

if You also want this script for practice purpose.

Then, Click Here For Downloading...

And For Complete Tutorial Series Click here

Thanks For Reading!.

For Any Type Of Suggestion or help

Article Written by Suraj