Home  >  Blog  >   Python

Python Regular Expression (RegEx) Cheatsheet

Rating: 4
  
 
5994
  1. Share:
Python Articles

Python RegEx:

The following section of the article is going to provide you with bits and pieces of Python code that will make your life a lot easier. This is intended to be a one stop shop for all the required regular expressions that you will ever encounter to use in your Python development.

Python Regular Expressions

The pieces of code are segregated into multiple tabular forms to provide you with a clear picture of its usage and also forms an easy/quick reference, once you master these cheat codes. Let us take a look at these codes with no further delay:

If you would like to become a Python certified professional, then visit Mindmajix - A Global online training platform:  Python Certification Training”  Course.  This course will help you to achieve excellence in this domain.

Python Regular Expressions Cheat Sheet:

The following tabular form provides details about how to apply regular expressions on characters of a string:

.

Matches any character except the newline character

^

Matches the beginning of the string provided

$

Matches the end of the string provided

*

Matches 0 or more repetitions of the provided character

+

Matches 1 or more repetitions of the provided character

?

Matches either 0 or 1 repetition of a character

 

*?

Matches 0 or more repetitions of the provided character in a non-greedy manner

+?

Matches 1 or more repetitions of the provided character in a non-greedy manner

??

Matches either 0 or 1 repetition of a character in a non-greedy manner

 

Escapes a special character

[]

Matches a set of characters provided within the square brackets

[a-z]

Matches any lower cased ASCII letters

[lower-upper]

Matches a set of characters from lower to upper cased ASCII letters

[^]

Matches a set of characters that are not in a set

A|B

Matches either regular expressions A or B in a non-greedy manner

 

{m}

Matches exactly m copies of the character specified

{m,n}

Matches from m to n repetitions of the character specified

{,n}

Matches from 0 to n repetitions of the character specified

{m,}

Matches from m to infinite repetitions of the character specified

{m,n}?

Matches from m to n repetitions in a non-greedy manner (as few as possible)

[Related Article: Regular expression operations

MindMajix Youtube Channel

Frequently Asked Python Interview Questions & Answers

The following tabular form provides the list of flags that are available for ready usage in the regular expressions.

(?)

Extension notation used to set flags

a

Flag to denote to match only ASCII characters

i

Flag to denote to ignore the case

L

Flag to denote that it is Locale dependent

m

Multi-line flag

s

Dot matches all flag

x

Verbose flag

u

Matches Unicode character classes

The following tabular form provides details about how to apply regular expressions on special sequences, altogether. 

A

Matches only at the beginning of the string

b

Matches an empty string, only at the beginning or end of a word

B

Matches an empty string, only when it is NOT at the beginning or end of a word

d

Matches digits same as [0-9]

D

Matches anything that is not a digit, same as [^0-9]

 

s

Matches white space characters, similar to [tnrfv]

S

Matches non-white space characters, similar to [^tnrfv]

w

Matches Unicode word characters, similar to [a-zA-Z0-9_]

W

Matches any character that is not a Unicode word character, similar to [^a-zA-Z0-9_]

Z

Matches only at the end of the given string

The following tabular form provides details about the RE Methods that are provided for us to use:

re.compile(pattern, flags)

Compiles a regular expression with a pattern along with flags

re.match(pattern, string)

Matches the pattern only at the beginning of the string

re.search(pattern, string)

Matches the pattern anywhere in the provided string

re.split(pattern, string)

Splits the string by the occurrences of the provided pattern

re.sub(pattern, str2, string)

Replaces the left-most non-overlapping occurrences of the provided pattern in the string with str2

 

re.fullmatch(pattern, string)

Matches the provided pattern only if the whole string matches the regular expression

re.findall(pattern, string)

Returns all non-overlapping matches of the provided pattern in string as a list of strings

re.finditer(pattern, string)

Returns an iterator that yields matched objects over non-overlapping matches of the provided pattern in string

re.subn(pattern, str2, string)

Replaces the left most occurrences of the given pattern in string with str2 and returns a tuple of (newsubstring, subs made)

re.purge()

Clears the regular expression cache of Python

The following tabular form provides details about the groups that we can readily use:

(match)

This is used to specify a group on which a match can be retrieved later

(?:match)

Non capturing version parenthesis, that is the match cannot be retrieved later

(?P)

Capture the group with name “name”

(?P=name)

Backtrack group named “name” in same pattern

(?#comment)

Denotes a comment

The following tabular form provides details about the match objects that we can readily use: 

match.group(“name”)

Returns the subgroup “name” of match

match.groups()

Returns tuples containing all subgroups of match

match.groupdict()

Returns dict containing all named subgroups of match

match.start(group)

Returns the start index of substring matched by group

match.end(group)

Returns the end index of substring matched by group

match.span(group)

Returns 2 tuple start and end indices of group in match

 

match.pos

Value of pos which was passed either to search() or match()

match.endpos

Value of endpos which was passed either to search() or match()

match.lastindex

Integer index of last matched capturing group

match.lastgroup

Name of the last matched capturing group

match.re

The regular expression who match() or search() created the match

match.string

The string that was passed to either match() or search()

 The following tabular form provides the look-ahead and behind feature for your ready usage:

(?=match)

Denotes a lookahead assertion – matches if contents matches next, but does not consume any of the string

(?!match)

This denotes a negative lookahead assertion – match if contents does not match next

(?<=match)

Positive lookbehind assertion – matches if the current position in the string is preceded by the match

(?

Negative lookbehind assertion – matches if the current position is not preceded by the match

(?(id/name)yes|no)

Matches a “yes” pattern if “id” or “name” exists or else, matches “no” pattern

Conclusion:

Though there has been great care taken while providing the best of the information, please be aware that this has been picked up for Python 2.7 and for earlier versions, please do check the official Python documentation.

If you are interested to learn Python and to become an Python Expert? Then check out our Python Certification Training Course at your near Cities.

Python Course ChennaiPython Course BangalorePython Course DallasPython Course Newyork

These courses are incorporated with Live instructor-led training, Industry Use cases, and hands-on live projects. This training program will make you an expert in Python and help you to achieve your dream job.

Explore Python Sample Resumes! Download & Edit, Get Noticed by Top Employers!Download Now!

 

Join our newsletter
inbox

Stay updated with our newsletter, packed with Tutorials, Interview Questions, How-to's, Tips & Tricks, Latest Trends & Updates, and more ➤ Straight to your inbox!

Course Schedule
NameDates
Python TrainingApr 30 to May 15View Details
Python TrainingMay 04 to May 19View Details
Python TrainingMay 07 to May 22View Details
Python TrainingMay 11 to May 26View Details
Last updated: 03 Apr 2023
About Author

Anjaneyulu Naini is working as a Content contributor for Mindmajix. He has a great understanding of today’s technology and statistical analysis environment, which includes key aspects such as analysis of variance and software,. He is well aware of various technologies such as Python, Artificial Intelligence, Oracle, Business Intelligence, Altrex, etc. Connect with him on LinkedIn and Twitter.

read more
Recommended Courses

1 / 15