Regular Expression Tutorial With Python - part 1 - python regex tutorial

Posted by Suraj Singh on January 17, 2017 · 7 mins read
Hello readers,

                                         Today, I Decided To Write A Python Related Topics In Series Wise. Because, hmm, i thinks,  A Series of Post One By One Can Explain Any Difficult Topic Easily and I can explain topics more clearly. 

So, let's Start Our Today Tutorials. Before Starting I want to notify you That I had taken some information from overall internet sites. 

               In This post, I am going to write about how we can use regular expression with python? or you can also say how to use re module in python?

So, First we will talk about

Q 1. What is Regular Expression?.

Ans. In Simple word, Regular Expression (Also Known As regex, regexp and sometimes called a rational expression) is an special technique for search any data, content, line, text, specially formatted text .etc.. 
and believe me, regular expression is really a juicy and useful topic. so, keep reading..

Q 2. Usages of Regular Expression?
Ans. There are many usages but some of them given below:
  •  For Finding texts using patterns
  • For checking input data patterns
  • for extracting data in special format
  • for extracting specific portions of a text
  • for dividing large piece of texts in smaller parts... etc etc.

with regular expression, we can create an special text processing applications and many types of other applications for different purposes.

Q 3. How To Regular Expressions Works?

Ans. I will try my best to explain all topics. so, in simple word, regular expressions works like normal find or search functions but in more power full way. regular expressions can contain both normal and special characters. normal character means if we want to search any keyword like dog. so, we can do with "dog" expression. but if we want to search dog and cat. then there is a special character " | "  use for or functions. with this character we can search both dog and cat with one expression like "dog|cat". 

hmm, now you thinking that this is really a juicy topic. 

Some special characters are:
abc Letters
123    Digits
\d      Any Digit
\D      Any Non-digit character
.       Any Character
\.      Period
[abc]   Only a, b, or c
[^abc]  Not a, b, nor c
[a-z]   Characters a to z
[0-9]   Numbers 0 to 9
\w      Any Alphanumeric character
\W      Any Non-alphanumeric character
{m}     m Repetitions
{m,n}   m to n Repetitions
*       Zero or more repetitions
+       One or more repetitions
?       Optional character
\s      Any Whitespace
\S      Any Non-whitespace character
^...$     Starts and ends
(...)     Capture Group
(a(bc)) Capture Sub-group
(.*)    Capture all
(abc|def)   Matches abc or def

Some Useful Resource Links
For More Info About Regular Expression Syntax Click Here
For Online Testing Regular Expression Click here
or For Offline Testing, Download my script click here
Let's do some practical example.


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

And Output is 

Your Match Found : the in the man of steel can break that wall.

Your Match Found : steel in the man of steel can break that wall.
So, Today's Post Ends Here. For More Details Check Another Part.
For Second Part Click Here.

Have A Nice Day.