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

4 Python Regex Match Function

The Python regex match() function attempts to match a regex pattern to the beginning of a string. It returns a match object on success, or None on failure. The match object can be used to retrieve the matched text or subgroups using methods like group() and groups(). Examples show matching text at the start or end of strings, and using flags to ignore case or treat periods as any character.

Uploaded by

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

4 Python Regex Match Function

The Python regex match() function attempts to match a regex pattern to the beginning of a string. It returns a match object on success, or None on failure. The match object can be used to retrieve the matched text or subgroups using methods like group() and groups(). Examples show matching text at the start or end of strings, and using flags to ignore case or treat periods as any character.

Uploaded by

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

Python regex `match` function

Python Regex match() function explained with examples.

WE'LL COVER THE FOLLOWING

• The Match Function


• Match Flags
• Return values
• Example 1
• Example 2
• Example 3
• Example 4
• Example 5

The Match Function #


The match function attempts to match a re pattern to string with optional
flags.

Here is the syntax for this function −

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

Where, pattern is the regular expression to be matched, string is the string


to be searched to match the pattern at the beginning of string and flags ,
which you can specify different flags using bitwise OR ( | ).

Match Flags #

Modifier Description
Performs case-insensitive
re.I
matching.

Interprets words according to the


current locale. This interpretation
re.L affects the alphabetic group ( \w
and \W ), as well as word boundary
behavior ( \b and \B ).

Makes $ match the end of a line


re.M and makes ^ match the start of any
line.

Makes a period (dot) match any


re.S
character, including a newline.

Interprets letters according to the


Unicode character set. This flag
re.U
affects the behavior of \w , \W , \b ,
\B .

It ignores whitespace (except


inside a set [] or when escaped by
re.X
a backslash and treats unescaped
# as a comment marker.

Return values #
The re.match function returns a match object on success and None upon
failure. -
Use group(n) or groups() function of match object to get matched
expression, e.g., group(n=0) returns entire match (or specific subgroup
n)

The function groups() returns all matching subgroups in a tuple (empty


if there weren’t any).

Example 1 #
Let’s find the words before and after the word to :

#!/usr/bin/python
import re

line = "Learn to Analyze Data with Scientific Python";

m = re.match( r'(.*) to (.*?) .*', line, re.M|re.I)

if m:
print "m.group() : ", m.group()
print "m.group(1) : ", m.group(1)
print "m.group(2) : ", m.group(2)
else:
print "No match!!"

The first group (.*) identified the string: Learn and the next group (*.?)
identified the string: Analyze.

Example 2 #
groups([default]) returns a tuple containing all the subgroups of the match,
from 1 up to however many groups are in the pattern.

#!/usr/bin/python
import re

line = "Learn Data, Python";

m = re.match( r'(\w+) (\w+)', line, re.M|re.I)

if m:
print "m.group() : ", m.groups()
print "m.group (1,2)", m.group(1, 2)
else:
print "No match!!"

Example 3 #
groupdict([default]) returns a dictionary containing all the named
subgroups of the match, keyed by the subgroup name.

#!/usr/bin/python
!/us /b /pyt o
import re

number = "124.13";

m = re.match( r'(?P<Expotent>\d+)\.(?P<Fraction>\d+)', number)

if m:
print "m.groupdict() : ", m.groupdict()
else:
print "No match!!"

Example 4 #

Start, end. How can we match the start or end of a string? We can use the “A”
and “Z” metacharacters. We precede them with a backslash. We match strings
that start with a certain letter, and those that end with another.

import re

values = ["Learn", "Live", "Python"];

for value in values:


# Match the start of a string.
result = re.match("\AL.+", value)
if result:
print("START MATCH [L]:", value)

# Match the end of a string.


result2 = re.match(".+n\Z", value)
if result2:
print("END MATCH [n]:", value)

Example 5 #

start([group]) and end([group]) return the indices of the start and end of
the substring matched by group . See the next lesson for an example.

You might also like