0% found this document useful (0 votes)
8 views14 pages

Python Regex Syntax and Examples

This document is a comprehensive cheatsheet for Python regular expressions, detailing syntax, anchors, quantifiers, character classes, lookarounds, and flags. It provides examples of various functions from the re module, such as re.search, re.findall, and re.sub, along with practical use cases. The content is aimed at helping users understand and effectively utilize regular expressions in Python programming.

Uploaded by

kebdozoo
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)
8 views14 pages

Python Regex Syntax and Examples

This document is a comprehensive cheatsheet for Python regular expressions, detailing syntax, anchors, quantifiers, character classes, lookarounds, and flags. It provides examples of various functions from the re module, such as re.search, re.findall, and re.sub, along with practical use cases. The content is aimed at helping users understand and effectively utilize regular expressions in Python programming.

Uploaded by

kebdozoo
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

learnbyexample Books Mini Tips Tags About

Python regular expression cheatsheet and


examples
2020-07-03

Above visualization is a screenshot created using debuggex for the pattern r'\bpar(en|ro)?
t\b'

From [Link]: re:

A regular expression (or RE) specifies a set of strings that matches it; the functions in
this module let you check if a particular string matches a given regular expression

This blog post gives an overview and examples of regular expression syntax as implemented
by the re built-in module (Python 3.8+). Assume ASCII character set unless otherwise
specified. This post is an excerpt from my Python re(gex)? book.

Elements that define a regular expression🔗

Anchors Description
Anchors Description

\A restricts the match to the start of string

\Z restricts the match to the end of string

^ restricts the match to the start of line

$ restricts the match to the end of line

\n newline character is used as line separator

[Link] or re.M flag to treat input as multiline string

\b restricts the match to the start/end of words

word characters: alphabets, digits, underscore

\B matches wherever \b doesn't match

^ , $ and \ are metacharacters in the above table, as these characters have special meaning.
Prefix a \ character to remove the special meaning and match such characters literally. For
example, \^ will match a ^ character instead of acting as an anchor.

Feature Description

| multiple RE combined as conditional OR

each alternative can have independent anchors

(RE) group pattern(s), also a capturing group

a(b|c)d is same as abd|acd

(?:RE) non-capturing group

(?P<name>pat) named capture group

. Match any character except the newline character \n

[] Character class, matches one character among many


Greedy Quantifiers Description

* Match zero or more times

+ Match one or more times

? Match zero or one times

{m,n} Match m to n times (inclusive)

{m,} Match at least m times

{,n} Match up to n times (including 0 times)

{n} Match exactly n times

pat1.*pat2 any number of characters between pat1 and pat2

pat1.*pat2|pat2.*pat1 match both pat1 and pat2 in any order

Greedy here means that the above quantifiers will match as much as possible that'll also honor
the overall RE. Appending a ? to greedy quantifiers makes them non-greedy, i.e. match as
minimally as possible. Quantifiers can be applied to literal characters, groups, backreferences
and character classes.

Character class Description

[aeiou] Match any vowel

[^aeiou] ^ inverts selection, so this matches any consonant

[a-f] - defines a range, so this matches any of abcdef characters

\d Match a digit, same as [0-9]

\D Match non-digit, same as [^0-9] or [^\d]

\w Match word character, same as [a-zA-Z0-9_]

\W Match non-word character, same as [^a-zA-Z0-9_] or [^\w]

\s Match whitespace character, same as [\ \t\n\r\f\v]

\S Match non-whitespace character, same as [^\ \t\n\r\f\v] or [^\s]


Lookarounds Description

lookarounds custom assertions, zero-width like anchors

(?!pat) negative lookahead assertion

(?<!pat) negative lookbehind assertion

(?=pat) positive lookahead assertion

(?<=pat) positive lookbehind assertion

(?!pat1)(?=pat2) multiple assertions can be specified in any order

as they mark a matching location without consuming characters

((?!pat).)* Negate a grouping, similar to negated character class

Flags Description

[Link] or re.I flag to ignore case

[Link] or re.S allow . metacharacter to match newline character

flags=re.S|re.I multiple flags can be combined using | operator

[Link] or re.M allow ^ and $ anchors to match line wise

[Link] or re.X allows to use literal whitespaces for aligning purposes

and to add comments after the # character

escape spaces and # if needed as part of actual RE

[Link] or re.A match only ASCII characters for \b , \w , \d , \s

and their opposites, applicable only for Unicode patterns

[Link] or re.L use locale settings for byte patterns and 8-bit locales

(?#comment) another way to add comments, not a flag

(?flags:pat) inline flags only for this pat , overrides flags argument

flags is i for re.I , s for re.S , etc, except L for re.L


Flags Description

(?-flags:pat) negate flags only for this pat

(?flags-flags:pat) apply and negate particular flags only for this pat

(?flags) apply flags for whole RE, can be used only at start of RE

anchors if any, should be specified after (?flags)

Matched portion Description

[Link] object details like matched portions, location, etc

m[0] or [Link](0) entire matched portion of [Link] object m

m[n] or [Link](n) matched portion of nth capture group

[Link]() tuple of all the capture groups' matched portions

[Link]() start and end+1 index of entire matched portion

pass a number to get span of that particular capture group

can also use [Link]() and [Link]()

\N backreference, gives matched portion of Nth capture group

applies to both search and replacement sections

possible values: \1 , \2 up to \99 provided no more digits

\g<N> backreference, gives matched portion of Nth capture group

possible values: \g<0> , \g<1> , etc (not limited to 99)

\g<0> refers to entire matched portion

(?P<name>pat) named capture group

refer as 'name' in [Link] object

refer as (?P=name) in search section

refer as \g<name> in replacement section

groupdict method applied on a [Link] object


Matched portion Description

gives named capture group portions as a dict

\0 and \100 onwards are considered as octal values, hence cannot be used as
backreferences.

re module functions🔗

Function Description

[Link] Check if given pattern is present anywhere in input string

Output is a [Link] object, usable in conditional expressions

r-strings preferred to define RE

Use byte pattern for byte input

Python also maintains a small cache of recent RE

[Link] ensures pattern matches the entire input string

[Link] Compile a pattern for reuse, outputs [Link] object

[Link] search and replace

[Link](r'pat', f, s) function f with [Link] object as argument

[Link] automatically escape all metacharacters

[Link] split a string based on RE

text matched by the groups will be part of the output

portion matched by pattern outside group won't be in output

[Link] returns all the matches as a list

if 1 capture group is used, only its matches are returned

1+, each element will be tuple of capture groups

portion matched by pattern outside group won't be in output


Function Description

[Link] iterator with [Link] object for each match

[Link] gives tuple of modified string and number of substitutions

The function definitions are given below:

[Link](pattern, string, flags=0)


[Link](pattern, string, flags=0)
[Link](pattern, flags=0)
[Link](pattern, repl, string, count=0, flags=0)
[Link](pattern)
[Link](pattern, string, maxsplit=0, flags=0)
[Link](pattern, string, flags=0)
[Link](pattern, string, flags=0)
[Link](pattern, repl, string, count=0, flags=0)

Regular expression examples🔗

As a good practice, always use raw strings to construct RE, unless other formats are required.
This will avoid clash of special meaning of backslash character between RE and normal
quoted strings.

examples for [Link]

>>> sentence = 'This is a sample string'

# need to load the re module before use


>>> import re
# check if 'sentence' contains the pattern described by RE argument
>>> bool([Link](r'is', sentence))
True
# ignore case while searching for a match
>>> bool([Link](r'this', sentence, flags=re.I))
True
>>> bool([Link](r'xyz', sentence))
False

# [Link] output can be directly used in conditional expressions


>>> if [Link](r'ring', sentence):
... print('mission success')
...
mission success

# use raw byte strings if input is of byte data type


>>> bool([Link](rb'is', b'This is a sample string'))
True

difference between string and line anchors

# string anchors
>>> bool([Link](r'\Ahi', 'hi hello\ntop spot'))
True
>>> words = ['surrender', 'up', 'newer', 'do', 'ear', 'eel', 'pest']
>>> [w for w in words if [Link](r'er\Z', w)]
['surrender', 'newer']

# line anchors
>>> bool([Link](r'^par$', 'spare\npar\ndare', flags=re.M))
True

examples for [Link]

# whole word par with optional s at start and optional e at end


>>> [Link](r'\bs?pare?\b', 'par spar apparent spare part pare')
['par', 'spar', 'spare', 'pare']

# numbers >= 100 with optional leading zeros


>>> [Link](r'\b0*[1-9]\d{2,}\b', '0501 035 154 12 26 98234')
['0501', '154', '98234']

# if multiple capturing groups are used, each element of output


# will be a tuple of strings of all the capture groups
>>> [Link](r'([^/]+)/([^/,]+),?', '2020/04,1986/Mar')
[('2020', '04'), ('1986', 'Mar')]

# normal capture group will hinder ability to get whole match


# non-capturing group to the rescue
>>> [Link](r'\b\w*(?:st|in)\b', 'cost akin more east run')
['cost', 'akin', 'east']

# useful for debugging purposes as well


>>> [Link](r't.*?a', 'that is quite a fabricated tale')
['tha', 't is quite a', 'ted ta']

examples for [Link]

# split based on one or more digit characters


>>> [Link](r'\d+', 'Sample123string42with777numbers')
['Sample', 'string', 'with', 'numbers']

# split based on digit or whitespace characters


>>> [Link](r'[\d\s]+', '**1\f2\n3star\t7 77\r**')
['**', 'star', '**']

# to include the matching delimiter strings as well in the output


>>> [Link](r'(\d+)', 'Sample123string42with777numbers')
['Sample', '123', 'string', '42', 'with', '777', 'numbers']

# use non-capturing group if capturing is not needed


>>> [Link](r'hand(?:y|ful)', '123handed42handy777handful500')
['123handed42', '777', '500']

backreferencing within search pattern

# whole words that have at least one consecutive repeated character


>>> words = ['effort', 'flee', 'facade', 'oddball', 'rat', 'tool']

>>> [w for w in words if [Link](r'\b\w*(\w)\1\w*\b', w)]


['effort', 'flee', 'oddball', 'tool']

working with matched portions

>>> [Link](r'b.*d', 'abc ac adc abbbc')


<[Link] object; span=(1, 9), match='bc ac ad'>
# retrieving entire matched portion, note the use of [0]
>>> [Link](r'b.*d', 'abc ac adc abbbc')[0]
'bc ac ad'

# capture group example


>>> m = [Link](r'a(.*)d(.*a)', 'abc ac adc abbbc')
# to get matched portion of second capture group
>>> m[2]
'c a'
# to get a tuple of all the capture groups
>>> [Link]()
('bc ac a', 'c a')

examples for [Link]

# numbers < 350


>>> m_iter = [Link](r'[0-9]+', '45 349 651 593 4 204')
>>> [m[0] for m in m_iter if int(m[0]) < 350]
['45', '349', '4', '204']

# start and end+1 index of each matching portion


>>> m_iter = [Link](r'ab+c', 'abc ac adc abbbc')
>>> for m in m_iter:
... print([Link]())
...
(0, 3)
(11, 16)

examples for [Link]

>>> ip_lines = "catapults\nconcatenate\ncat"


>>> print([Link](r'^', r'* ', ip_lines, flags=re.M))
* catapults
* concatenate
* cat

# replace 'par' only at start of word


>>> [Link](r'\bpar', r'X', 'par spar apparent spare part')
'X spar apparent spare Xt'

# same as: r'part|parrot|parent'


>>> [Link](r'par(en|ro)?t', r'X', 'par part parrot parent')
'par X X X'

# remove first two columns where : is delimiter


>>> [Link](r'\A([^:]+:){2}', r'', 'foo:123:bar:baz', count=1)
'bar:baz'

backreferencing in replacement section


# remove consecutive duplicate words separated by space
>>> [Link](r'\b(\w+)( \1)+\b', r'\1', 'aa a a a 42 f_1 f_1 f_13.14')
'aa a 42 f_1 f_13.14'

# add something around the matched strings


>>> [Link](r'\d+', r'(\g<0>0)', '52 apples and 31 mangoes')
'(520) apples and (310) mangoes'

# swap words that are separated by a comma


>>> [Link](r'(\w+),(\w+)', r'\2,\1', 'good,bad 42,24')
'bad,good 24,42'

# example with both capturing and non-capturing groups


>>> [Link](r'(\d+)(?:abc)+(\d+)', r'\2:\1', '2abcabc42 12abcd21')
'42:2 12abcd21'

using functions in replacement section of [Link]

>>> from math import factorial


>>> numbers = '1 2 3 4 5'
>>> def fact_num(n):
... return str(factorial(int(n[0])))
...
>>> [Link](r'\d+', fact_num, numbers)
'1 2 6 24 120'

# using lambda
>>> [Link](r'\d+', lambda m: str(factorial(int(m[0]))), numbers)
'1 2 6 24 120'

examples for lookarounds

# change 'foo' only if it is not followed by a digit character


# note that end of string satisfies the given assertion
# foofoo has 2 matches as the assertion doesn't consume characters
>>> [Link](r'foo(?!\d)', r'baz', 'hey food! foo42 foot5 foofoo')
'hey bazd! foo42 bazt5 bazbaz'

# change whole word only if it is not preceded by : or -


>>> [Link](r'(?<![:-])\b\w+\b', r'X', ':cart <apple -rest ;tea')
':cart <X -rest ;X'

# match digits only if it is preceded by - and followed by ; or :


>>> [Link](r'(?<=-)\d+(?=[:;])', 'fo-5, ba3; x-83, y-20: f12')
['20']

# words containing 'b' and 'e' and 't' in any order


>>> words = ['sequoia', 'questionable', 'exhibit', 'equation']
>>> [w for w in words if [Link](r'(?=.*b)(?=.*e).*t', w)]
['questionable', 'exhibit']

# match if 'do' is not there between 'at' and 'par'


>>> bool([Link](r'at((?!do).)*par', 'fox,cat,dog,parrot'))
False
# match if 'go' is not there between 'at' and 'par'
>>> bool([Link](r'at((?!go).)*par', 'fox,cat,dog,parrot'))
True

examples for [Link]

Regular expressions can be compiled using [Link] function, which gives back a
[Link] object. The top level re module functions are all available as methods for this
object. Compiling a regular expression helps if the RE has to be used in multiple places or
called upon multiple times inside a loop (speed benefit). By default, Python maintains a small
list of recently used RE, so the speed benefit doesn't apply for trivial use cases.

>>> pet = [Link](r'dog')


>>> type(pet)
<class '[Link]'>
>>> bool([Link]('They bought a dog'))
True
>>> bool([Link]('A cat crossed their path'))
False

>>> pat = [Link](r'\([^)]*\)')


>>> [Link]('', 'a+b(addition) - foo() + c%d(#modulo)')
'a+b - foo + c%d'
>>> [Link]('', 'Hi there(greeting). Nice day(a(b)')
'Hi there. Nice day'

Python re(gex)? book🔗


Visit my GitHub repo Python re(gex)? for details about the book I wrote on Python regular
expressions. The ebook uses plenty of examples to explain the concepts from the very
beginning and step by step introduces more advanced concepts. The book also covers the third
party module regex. The cheatsheet and examples presented in this post are based on contents
of this book.

Use this leanpub link for a discounted price.

#python #regular-expressions #cheatsheet #re-module #examples

← JavaScript regular expressions cheatsheet and examples

Example driven book on Python regular expressions →


📰 Use this link for the Atom feed.
✅ Follow me on Twitter, GitHub and Youtube for interesting tech nuggets.
📧 Subscribe to learnbyexample weekly for programming resources, tips, tools, free ebooks
and more (free newsletter, delivered every Friday).

You might also like