Docsity

Python RegEx Cheatsheet with Examples, Cheat Sheet of Programming Languages

Toggle sidebar
bg1
re Module Functions
Read the documentation here:
https://docs.python.org/3/library/re.html
Need more help?
A RegEx, or Regular Expression, is a sequence of characters that forms a search pattern. They’re
typically used to find a sequence of characters within a string so you can extract and manipulate them.
For example, the following returns both instances of ‘active’:
import re
pattern = 'ac..ve'
test_string = 'my activestate platform account is now active'
result = re.findall(pattern, test_string)
Python RegEx Cheatsheet with Examples
Quantifiers
match m to n occurrences,
but as few as possible (eg., py{1,3}?)
{m,n}
match m to infinite occurrences
(eg., py{3,})
{m,}
match from 0 to n occurrences
(eg., py{,3})
{,n}
match from m to n occurrences
(eg., py{1,3})
{m,n}
match exactly m occurrences
(eg., py{3})
{m}
match 0 or 1 occurrences
(eg., py?)
?
match 1 or more occurrences
(eg., py+)
+
match 0 or more occurrences
(eg., py*)
*
Special characters
.
^
$
[3a-c]
[^x-z1]
A|S
()
\
match any char except newline
(eg., ac..ve)
match at beginning of string
(eg., ^active)
match at end of string
(eg, state$)
match any char
(ie., 3 or a or b or c)
match any char except x, y, z or 1
match either A or S regex
capture & match a group of chars
(eg., (8097ba))
escape special characters
match occurrence only at start of string
match occurrence only at end of string
match empty string at word boundary (e.g.,
between \w and \W)
match empty string not at word boundary
match a digit
match a non-digit
match any whitespace char: [ \t\n\r\f\v]
match any non-whitespace char
match any alphanumeric: [0-9a-zA-Z_]
match any non-alphanumeric
matches a previously captured group
match expression represented by A
(non-capture group)
match expression A only if followed by B
match expression A only if not followed by
B
match expression A only if it follows B
match expression A only if not preceded by
B
where a, i, L, m, s, u, and x are flags:
Special sequences
\A
\Z
\b
\B
\d
\D
\s
\S
\w
\W
\g<id>
(?:A)
A(?=B)
A(?!B)
(?<=B)A
(?<!B)A
(?aiLmsux)
match ASCII only
make matches ignore case
make matches locale dependent
multi-line makes ^ and $ match at the
beginning/end of each line, respectively
makes ‘.’ match any char including newline
match unicode only
verbose increases legibility by allowing
comments & ignoring most whitespace
a =
i =
L =
m =
s =
u =
x =
Besides enabling the above functionality, the ‘re’ module also features a number of popular functions:
re.findall(A, B) match all occurrences of expression A in string B
re.search(A, B) match the first occurrence of expression A in string B
re.split(A, B) split string B into a list using the delimiter A
re.sub(A, B, C) replace A with B in string C
www.activestate.com
RegExes are extremely useful, but the syntax can be hard to recall. With that in mind, ActiveState offers
this “cheatsheet” to help point you in the right direction when building RegExes in Python.

Partial preview of the text

Download Python RegEx Cheatsheet with Examples and more Cheat Sheet Programming Languages in PDF only on Docsity!

re Module Functions

Read the documentation here:

https://docs.python.org/3/library/re.html

Need more help?

A RegEx, or Regular Expression, is a sequence of characters that forms a search pattern. They’re

typically used to find a sequence of characters within a string so you can extract and manipulate them.

For example, the following returns both instances of ‘active’:

import re

pattern = 'ac..ve'

test_string = 'my activestate platform account is now active'

result = re.findall(pattern, test_string)

Python RegEx Cheatsheet with Examples

Quantifiers

match m to n occurrences,

{m,n} but as few as possible (eg., py{1,3}?)

match m to infinite occurrences

{m,} (eg., py{3,})

match from 0 to n occurrences

{,n} (eg., py{,3})

match from m to n occurrences

{m,n} (eg., py{1,3})

match exactly m occurrences

{m} (eg., py{3})

match 0 or 1 occurrences ? (eg., py?)

match 1 or more occurrences

  • (eg., py+)

match 0 or more occurrences

  • (eg., py*)

Special characters

^

[3a-c]

[^x-z1]

A|S

\

match any char except newline (eg., ac..ve) match at beginning of string (eg., ^active) match at end of string (eg, state$) match any char (ie., 3 or a or b or c) match any char except x, y, z or 1 match either A or S regex capture & match a group of chars (eg., (8097ba)) escape special characters

match occurrence only at start of string match occurrence only at end of string match empty string at word boundary (e.g., between \w and \W) match empty string not at word boundary match a digit match a non-digit match any whitespace char: [ \t\n\r\f\v] match any non-whitespace char match any alphanumeric: [0-9a-zA-Z_] match any non-alphanumeric matches a previously captured group match expression represented by A (non-capture group) match expression A only if followed by B match expression A only if not followed by B match expression A only if it follows B match expression A only if not preceded by B where a, i, L, m, s, u, and x are flags:

Special sequences

\A

\Z

\b

\B

\d

\D

\s

\S

\w

\W

\g

(?:A)

A(?=B)

A(?!B)

(?<=B)A

(?<!B)A

(?aiLmsux)

match ASCII only make matches ignore case make matches locale dependent multi-line makes ^ and $ match at the beginning/end of each line, respectively makes ‘.’ match any char including newline match unicode only verbose increases legibility by allowing comments & ignoring most whitespace

a =

i =

L =

m =

s =

u =

x =

Besides enabling the above functionality, the ‘re’ module also features a number of popular functions:

re.findall(A, B) match all occurrences of expression A in string B

re.search(A, B) match the first occurrence of expression A in string B

re.split(A, B) split string B into a list using the delimiter A

re.sub(A, B, C) replace A with B in string C

www.activestate.com

RegExes are extremely useful, but the syntax can be hard to recall. With that in mind, ActiveState offers

this “cheatsheet” to help point you in the right direction when building RegExes in Python.