0% found this document useful (0 votes)
6 views7 pages

Python Regex Basics and Usage Guide

The document provides an introduction to regular expressions (regex) in Python, explaining how regex is used to match patterns in text. It details various metacharacters and their uses, such as character classes, repetitions, and anchors, as well as capturing and named groups. Additionally, it covers lookahead and lookbehind assertions for advanced matching scenarios.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views7 pages

Python Regex Basics and Usage Guide

The document provides an introduction to regular expressions (regex) in Python, explaining how regex is used to match patterns in text. It details various metacharacters and their uses, such as character classes, repetitions, and anchors, as well as capturing and named groups. Additionally, it covers lookahead and lookbehind assertions for advanced matching scenarios.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Introduction to Python (Regular expression)

regex

In general, regex or regular expression consists of special sequence of characters defining a


pattern used for matching it in a given text.

While searching a substring in a string requires it to match character by character or ‘literal


match’. For instance, finding a pattern pat=‘emp’ in STRING=‘Temp’ will return True as pat
matched in STRING. However, if one needs to find pattern such that substring starting with ‘e’
and ending with ‘p’ and any character can occur between them. Here, the regular expression
will be required for identify such complex patterns.
Example: matching email id, which will involve at least a NAME @ DOMAIN . COM

Python has a module implementing regular expression (re). The re module can be imported to
search for matching of pattern in string.

[Link]() scans the search string from left to right, and as soon as it locates a match for
<regex>, it stops scanning and returns the match.

1
12/03/24
Introduction to Python (Regular expression)
Literal characters: Characters as it is. It matches the first occurrence of the pattern character.
Case sensitive.

Metacharacters or special character: Certain characters are reserved for special use. These are
listed in the table.

Metacharacter Usage
[] Specify a character class or character set. [ap] – can match a or p
{} Matches explicit specified number of repetitions of the regex before
{}. 9{2} matches string containing substring ’99’
() Create groups
| Alteration. (mat|pat) will match mat or pat
+ Matches one or more repetitions of the regex before it.
* Matches zero or more repetitions of the regex preceding it. a*, will
match one more occurrence starting with a

2
12/03/24
Introduction to Python (Regular expression)

Metacharacter Usage
? - It matches zero or one repetition
- It specifies the non-greedy versions of *, +, and ?
- Creates a named group,
- Lookahead/lookbehind assertions
. Matches any character except new line
\ Escape a matacharacter for matching; special character class;
grouping backreference.
<> Named group
\w Matches any alphanumeric word character
\W Opposite of \w. Matches non-word character
\d Matches any decimal character
\D Is opposite of \d. Matches any character that is non-digit
\s Matches any whitespace character
\S Matches any non-whitespace character

3
12/03/24
Introduction to Python (Regular expression)

Metacharacter Usage
^ - Anchors matches the position before the first character in string.
^d to ‘def’ matches d
- Complements a character.
$ Anchors matches the last position in the string f$ applied to ‘def’
\b Anchors matches to a word boundary
\B Anchor matches to a non-word boundary

4
12/03/24
Introduction to Python (Regular expression)
Capturing groups:

By default the () creates capturing group with count starting from 1 … 99

The groups can be followed with quantifiers (* /+ /? )


e.g
(\d\d)+: means match repeats of two digits. Importantly, it will capture the
last two digit.
[Link](‘(\d+)’,’abc1234’); the capturing group will have 1234
[Link](‘(\d)+’,’abc1234’); the capturing group will have 4 (the last digit)
as the grouped digits are being repeated one more times.
In the groups()-> the first entry will be the first captured group.

The groups can be made non-capturing:

(:?regex)
In this case the groups are not be considered for group counting.

5
12/03/24
Introduction to Python (Regular expression)
Named capturing group:

The names capturing group can be assigned using following format


(?P<NAME>regex)

The named group can be recalled using (?P=NAME) for backreferencing

Backreferences allow matching of the same regex matching as previously identified by a


capturing group.

Lookahead and lookbehind assertions (zero-width)

Lookahead and lookbehind assertion determines the success or failure of a regex match based
on the presence of (text/regex) ahead(right) or behind(left) of the matching regex.

Lookahead:
regexA(?=regexB) The match will return only if regexA is followed by
regexB

regexA(?!regexB) The match will return only if regexA is NOT followed


by regexB 6
12/03/24
Introduction to Python (Regular expression)
Lookahead and lookbehind assertions (zero-width)

Lookbehind:
(?<=regexB)regexA The match will return only if regexA is followed by
regexB

(?<!regexB)regexA The match will return only if regexA is NOT followed


by regexB

7
12/03/24

You might also like