How To Use Regular Expression With Python - part 1 - python regex tutorial

Hello Friends,

                      From Today, I Decided To Start Writing Python Related Topics In Serial Wise. Because, hmm, i thinks, with these serial wise posts. i can explain topics more clearly. So, i have taken some information from overall internet sites.

Let's Start Today,
               This post is 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

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.

I think,this is enough for today.

In Our Tutorial, We will Discuss About Some Other Functions.

For Second Part Click Here.

Thanks For Reading!.

For Any Type Of Suggestion or help
please comment below, 
by S.S.B 
Email :

Share this

Related Posts

Next Post »