python tutorial - Python Regular Expression | Regular Expressions in Python | Part 2 - Search, Match and Find All - learn python - python programming

 python string regexp

Learn Python - Python tutorial - python string regexp - Python examples - Python programs

The module re provides support for regular expressions in Python. Below are main methods in this module.

python regex

Searching an occurrence of pattern : This method either returns None (if the pattern doesn’t match), or a re.MatchObject that contains information about the matching part of the string. This method stops after the first match, so this is best suited for testing a regular expression more than extracting data.

python - Sample - python code :

# A Python program to demonstrate working of re.match().
import re
# Lets use a regular expression to match a date string
# in the form of Month name followed by day number
regex = r"([a-zA-Z]+) (\d+)"
match =, "I was born on June 24")
if match != None:
    # We reach here when the expression "([a-zA-Z]+) (\d+)"
    # matches the date string.
    # This will print [14, 21), since it matches at index 14
    # and ends at 21. 
    print "Match at index %s, %s" % (match.start(), match.end())
    # We us group() method to get all the matches and
    # captured groups. The groups contain the matched values.
    # In particular:
    # always returns the fully matched string
    #, ... return the capture
    #    groups in order from left to right in the input string
    # is equivalent to
    # So this will print "June 24"
    print "Full match: %s" % (
    # So this will print "June"
    print "Month: %s" % (
    # So this will print "24"
    print "Day: %s" % (
    print "The regex pattern does not match."

python tutorial - Output :

Match at index 14, 21
Full match: June 24
Month: June
Day: 24 

Matching a Pattern with Text

re.match() : This function attempts to match pattern to whole string. The re.match function returns a match object on success, None on failure.


python - Sample - python code :

re.match(pattern, string, flags=0)

pattern : Regular expression to be matched.
string : String where p attern is searched
flags : We can specify different flags 
        using bitwise OR (|).

python - Sample - python code :

# A Python program to demonstrate working
# of re.match().
import re
# a sample function that uses regular expressions
# to find month and day of a date.
def findMonthAndDate(string):
    regex = r"([a-zA-Z]+) (\d+)"
    match = re.match(regex, string)
    if match == None: 
        print "Not a valid date"
    print "Given Data: %s" % (
    print "Month: %s" % (
    print "Day: %s" % (
# Driver Code
findMonthAndDate("Jun 24")
findMonthAndDate("I was born on June 24")

Finding all occurrences of a pattern

regular expression patterns summary

re.findall() : Return all non-overlapping matches of pattern in string, as a list of strings. The string is scanned left-to-right, and matches are returned in the order found (Source : Python Docs).

python - Sample - python code :

# A Python program to demonstrate working of
# findall()
import re
# A sample text string where regular expression 
# is searched.
string  = """Hello my Number is 123456789 and
             my friend's number is 987654321"""
# A sample regular expression to find digits.
regex = '\d+'            
match = re.findall(regex, string)

python tutorial - Output :

['123456789', '987654321']

Regular expression is a vast topic. It’s a complete library. Regular expressions can do a lot of stuff. You can Match, Search, Replace, Extract a lot of data. For example, below small code is so powerful that it can extract email address from a text. So we can make our own Web Crawlers and scrappers in python with easy.Look at below regex.

python - Sample - python code :

# extract all email addresses and add them into the resulting set
new_emails = set(re.findall(r"[a-z0-9\.\-+_]+@[a-z0-9\.\-+_]+\.[a-z]+", 
                           text, re.I))

Wikitechy tutorial site provides you all the learn python , python code , python script , python windows , python mysql , django python , python django , python compiler , jython , python interpreter , what is python , python join , python input , python modules , python editor , python examples , python pdf , python documentation

Related Searches to Regular Expressions in Python | Part 2 - Search, Match and Find All