RegEx

Chaayagirimon
featurepreneur
Published in
1 min readMar 19, 2022

RegEx stands for regular expression. It is used to search patterns in a particular string.

Import the built in re module to use regular expressions:

import re

There are different functions used to search patterns:

search -Returns a match if there is any match in the string.

findall -Returns a list of all the matches in the string

split -Returns a list where the string has been split at each match

sub -Replacing the matches with a string

Characters used to match patterns

Special sequences:

\d — Digit (0–9)
\D — Not a Digit (0–9)
\w — Word Character (a-z, A-Z, 0–9, _)
\W — Not a Word Character
\s — Whitespace (space, tab, newline)
\S — Not Whitespace (space, tab, newline)
\b — Word Boundary
\B — Not a Word Boundary

Meta characters:

^ — Beginning of a String
$ — End of a String
[ ] — Matches Characters in brackets
[^ ] — Matches Characters NOT in brackets
| — Either Or
( ) — Group
. — Any Character Except New Line

Quantifiers:

* — 0 or More
+ — 1 or More
-? — 0 or One

--

--