0% found this document useful (0 votes)
19 views

23.python Regular Expressions

The document discusses the re module in Python which provides support for regular expressions. It explains common regex functions like match, search, findall, split and sub. Examples are provided for each function to demonstrate their usage.

Uploaded by

MANO
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

23.python Regular Expressions

The document discusses the re module in Python which provides support for regular expressions. It explains common regex functions like match, search, findall, split and sub. Examples are provided for each function to demonstrate their usage.

Uploaded by

MANO
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

PYTHON REGULAR EXPRESSIONS

The regular expressions can be defined as the sequence of characters which are
used to search for a pattern in a string. The module re provides the support to use regex in
the python program

The re module must be imported to use the regex functionalities in python.

import re   

Regex Functions

The following regex functions are used in the python.

SN Function Description

This method matches the regex pattern in the string with the optional flag. It
1 Match
returns true if a match is found in the string otherwise it returns false.

2 Search This method returns the match object if there is a match found in the string.

3 Findall It returns a list that contains all the matches of a pattern in the string.

4 Split Returns a list in which the string has been split in each match.

5 Sub Replace one or many matches in the string.


THE FINDALL() FUNCTION

This method returns a list containing a list of all matches of a pattern within the
string. It returns the patterns in the order they are found. If there are no matches, then an
empty list is returned.

Consider the following example.

Example

import re  

str = "How are you. How is everything"  

matches = re.findall("How", str)  

print(matches)  

Output:

['How', 'How']

The search() Function

The search() function searches the string for a match, and returns a Match object if
there is a match.

If there is more than one match, only the first occurrence of the match will be
returned:

Example

>>> str = "hihappy how. how how everything"


>>> x = re.search("\s", str)
>>> print("The first white-space character is located in position:", x.start())
The first white-space character is located in position: 2

THE MATCH OBJECT

The match object contains the information about the search and the output. If there is no
match found, the None object is returned.

Example

import re  

str = "How are you. How is everything"  

matches = re.search("How", str)  

print(type(matches))  

print(matches) #matches is the search object  

Output:

<class '_sre.SRE_Match'>
<_sre.SRE_Match object; span=(0, 3), match='How'>

Example
>>>import re
>>> str = "hi happy how. how how everything"
>>> matches = re.search("how", str)
>>> print(matches)
<_sre.SRE_Match object; span=(9, 12), match='how'>
THE MATCH OBJECT METHODS

There are the following methods associated with the Match object.

span(): It returns the tuple containing the starting and end position of the match.

group(): The part of the string is returned where the match is found.

string: It returns a string passed into the function.

Example

import re  

str = "How are you. How is everything"  

matches = re.search("How", str)  

print(matches.span())  

print(matches.group())  

print(matches.string)  

Output:
(0, 3)
How
How are you. How is everything

SPLIT FUNCTION

Example 1: Split String by Regular Expression

In this example, we will take a string with items/words separated by a combination of


underscore and comma.

So, the delimiter could be __, _,, ,_ or ,,. The regular expression to cover these delimiters
is '[_,][_,]'. [_,] specifies that a character could match _ or ,.

Python Program

import re
str = '63__foo,,bar,_mango_,apple'
#split string into chunks
chunks = re.split('[_,][_,]',str)
#print chunks
print(chunks)

Output

['63', 'foo', 'bar', 'mango', 'apple']

Example 2: Split String by a Class


Regular expression classes are those which cover a group of characters. We will use one
of such classes, \d which matches any decimal digit.

In this example, we will also use + which matches one or more of the previous character.

Regular expression '\d+' would match one or more decimal digits. In this example, we
will use this regular expression to split a string into chunks which are separated by one or
more decimal digits.

Python Program

import re
str = 'foo635bar4125mango2apple21orange'
chunks = re.split('\d+',str)
print(chunks)

Output

['foo', 'bar', 'mango', 'apple', 'orange']

Example

>>> str = "hi happy how. how how everything"

>>> matches = re.split("how", str)

>>> print(matches)

['hi happy ', '. ', ' ', ' everything']

Example 2:

>>> str = "hi happy how. how how everything"


>>> matches=re.split("\s", str)

>>> print(matches)

['hi', 'happy', 'how.', 'how', 'how', 'everything']

THE SUB() FUNCTION

The sub() function replaces the matches with the text of your choice:

>>> str = "hi happy how. how how everything"

>>> matches=re.sub("\s", "8",str)

>>> print(matches)

hi8happy8how.8how8how8everything

Example

Replace the first 2 occurrences:

>>> str = "hi happy how. how how everything"

>>> matches=re.sub("\s", "8",str,3)

>>> print(matches)

hi8happy8how.8how how everything

You might also like