35. By OM PRAMOD
34
The Python Regex Handbook: Your Ultimate Cheatsheet
[^0-3]
[0123]
36. By OM PRAMOD
35
The Python Regex Handbook: Your Ultimate Cheatsheet
[0-9]
[0-5][0-9]
37. By OM PRAMOD
36
The Python Regex Handbook: Your Ultimate Cheatsheet
[a-zA-Z]
38. By OM PRAMOD
37
The Python Regex Handbook: Your Ultimate Cheatsheet
[a-zA-Z0-9]
{n}
39. By OM PRAMOD
38
The Python Regex Handbook: Your Ultimate Cheatsheet
{n,}
{,m}:
{n,m}
40. By OM PRAMOD
39
The Python Regex Handbook: Your Ultimate Cheatsheet
[]
Square
brackets
In regular expressions, square brackets ([]) are used to define a character set, which
is a set of characters you want to match. This can be useful when you want to match
any one character from a specific set of characters.
You can also use square brackets to match any character that is not in a specific set.
This is done by placing a caret (^) at the start of the character set
In addition to individual characters, you can also use a hyphen (-) inside square
brackets to specify a range of characters.
However, if you want to include a hyphen in your character set, you need to place it
at the start or end of the character set. This is because the hyphen is used to denote
a range in regular expressions. Placing it at the start or end ensures it is interpreted
as the literal character - and not as a range specifier.
41. By OM PRAMOD
40
The Python Regex Handbook: Your Ultimate Cheatsheet
In some cases, you might want to match the square brackets themselves as
characters. This can be done by escaping the square brackets with a backslash ().This
is because square brackets have a special meaning in regular expressions, denoting a
character set. By using a backslash, you’re indicating that you want to match the literal
character and not use its special meaning. For instance, For example, the regular
expression [ will match a single opening square bracket, and ] will match a single
closing square bracket.
. Period
In regular expressions, the dot (.) is a special character that matches any single
character except newline characters(This is an important aspect of regular
expressions to keep in mind when matching across multiple lines.). This means it can
represent any character from the alphabet, numbers, special characters, etc.
42. By OM PRAMOD
41
The Python Regex Handbook: Your Ultimate Cheatsheet
If you want to match a literal dot (.), you need to escape it using a backslash (). For
example, the regular expression . will match a single dot. This is because inside a
character class, the dot (.) loses its special meaning and matches a literal dot.
^ Caret
In regular expressions, the caret (^) is a special character that has different meanings
depending on its usage. Start of Line or String: When used at the start of a regular
expression, the caret (^) represents the start of a line or a string. For example, the
regular expression ^hello will match any string that starts with the word "hello".
Negation in Character Sets: When used inside square brackets ([]), the caret (^)
negates the set of characters. This means it will match any character that is not in the
set. For example, the regular expression [^abc] will match any character that is not 'a',
'b', or 'c'.
Literal Caret Character: If you want to match the actual caret (^) character, you need
to escape it using a backslash (). For example, the regular expression ^ will match a
single caret.
43. By OM PRAMOD
42
The Python Regex Handbook: Your Ultimate Cheatsheet
$ Dollar
In regular expressions, the dollar sign ($) is a special character that represents the end
of a line or a string.
* Star
In regular expressions, the asterisk (*) is a special character known as a quantifier that
specifies that the preceding character or group should be matched zero or more
times. This means it can match the preceding character or group any number of times,
including not at all.
If you want to match a literal asterisk (*), you need to escape it using a backslash ().
For example, the regular expression * will match a single asterisk. This is because
inside a character class, the asterisk (*) loses its special meaning and matches a literal
asterisk.
44. By OM PRAMOD
43
The Python Regex Handbook: Your Ultimate Cheatsheet
+ Plus
In regular expressions, the plus sign (+) is a special character known as a quantifier
that specifies that the preceding character or group should be matched one or more
times. This means it can match the preceding character or group at least once, but
possibly more.
If you want to match a literal plus sign (+), you need to escape it using a backslash ().
For example, the regular expression + will match a single plus sign. This is because
inside a character class, the plus sign (+) loses its special meaning and matches a literal
plus sign. This is an example of how escaping works in regular expressions. Escaping
is used to treat special characters as literal characters.
Inside a character class (denoted by square brackets []), the plus sign + loses its special
meaning and matches a literal plus sign. For example, the regular expression [+] will
match a single plus sign. This is because inside a character class, special characters
lose their special meaning and are treated as literal characters. This is another
important aspect of regular expressions to keep in mind.
45. By OM PRAMOD
44
The Python Regex Handbook: Your Ultimate Cheatsheet
?
Question
Mark
In regular expressions, the question mark (?) is a special character known as a
quantifier that specifies that the preceding character or group should be matched
zero or one times. This means it can match the preceding character or group either
once or not at all.
If you want to match a literal question mark (?), you need to escape it using a
backslash (). For example, the regular expression ? will match a single question
mark. This is because inside a character class, the question mark (?) loses its special
meaning and matches a literal question mark. Similarly, inside a character class
(denoted by square brackets []), most special characters lose their special meaning
and are treated as literal characters. So, [?] would also match a literal question mark
(?).
In addition to its role as a quantifier, the question mark (?) can also be used in certain
contexts to make a group optional or to apply modifiers to only part of the regular
expression. Optional Grouping: You can use the question mark to make an entire
46. By OM PRAMOD
45
The Python Regex Handbook: Your Ultimate Cheatsheet
group optional. This is done by surrounding the part of the pattern that should be
optional with parentheses (), and placing a question mark after the closing parenthesis
Modifiers: The question mark can also be used to apply modifiers to only part of the
regular expression. This is done by placing the modifier after a question mark inside
parentheses. In regular expressions, (?i) is a mode modifier that makes the rest of the
pattern case-insensitive. This means that the pattern will match regardless of whether
the characters are in uppercase or lowercase. For example, the pattern (?i) abc will
match abc, ABC, Abc, aBc, abC, ABc, aBC, and AbC. It’s a very useful feature when you
want to match a string regardless of its case. Please note that the ?i should be
enclosed in parentheses like this: (?i).
47. By OM PRAMOD
46
The Python Regex Handbook: Your Ultimate Cheatsheet
The (?=) syntax in regular expressions is known as a positive lookahead assertion. It is
used to assert that a certain pattern is ahead in the string, but it does not consume
any of the string. This means that the current position in the string remains the same
after the lookahead assertion. Lookahead assertions are particularly useful when you
want to match a pattern only if it's followed by another pattern, but you don't want
to include the second pattern in the match. For example, consider the regular
expression q(?=u)i. This expression will match a 'q' that is followed by a 'u', without
making the 'u' part of the match. The positive lookahead construct is a pair of
parentheses, with the opening parenthesis followed by a question mark and an equals
sign.
{} Braces
In regular expressions, curly braces ({}) are used as quantifiers to specify the exact
number of times a character or group should be matched. They are used in the format
{n}, where n is the exact number of times the preceding character or group should be
matched.
48. By OM PRAMOD
47
The Python Regex Handbook: Your Ultimate Cheatsheet
If you want to match a literal pair of curly braces ({}), you need to escape them using
a backslash (). For example, the regular expression {} will match a single pair of
curly braces. This is because inside a character class, the curly braces ({}) lose their
special meaning and match a literal pair of curly braces. Similarly, inside a character
class (denoted by square brackets []), most special characters lose their special
meaning and are treated as literal characters. So, [{}] would also match a literal pair
of curly braces ({}).
Curly braces can also be used in the format {n,} to match the preceding character or
group at least n times. For example, the regular expression a{2,} will match "aa",
"aaa", "aaaa", and so on.
the {,m} quantifier in regular expressions specifies that the preceding character or
group should be matched at most m times. Here m is a positive integer. If the
character or group appears more than m times, it will not be a match.
49. By OM PRAMOD
48
The Python Regex Handbook: Your Ultimate Cheatsheet
Similarly, curly braces can be used in the format {n,m} to match the preceding
character or group at least n times and at most m times. For example, the regular
expression a{2,3} will match "aa", "aaa"
|
Alternatio
n
In regular expressions, the pipe symbol (|) is used as an alternation operator, which
is similar to the logical OR operator. It allows you to match either the pattern before
the pipe or the pattern after the pipe.
() Group
In regular expressions, parentheses (()) are used for grouping and capturing.
Grouping: Parentheses are used to group parts of a regular expression together. This
allows you to apply a quantifier to the entire group or to restrict alternation to part of
the regular expression. Parentheses group parts of the pattern together. This allows
you to apply quantifiers to multiple characters instead of just one. For example, in the
pattern (abc)+, the + applies to the entire string abc, not just the character c.
50. By OM PRAMOD
49
The Python Regex Handbook: Your Ultimate Cheatsheet
Capturing: Besides grouping part of a regular expression together, parentheses also
create a numbered capturing group. It stores the part of the string matched by the
part of the regular expression inside the parentheses.
51. By OM PRAMOD
50
The Python Regex Handbook: Your Ultimate Cheatsheet
The (?i) keyword in regular expressions is used to perform case-insensitive matching.
When included in a regular expression, it makes the expression match the target string
regardless of the case of the characters. The (?i) keyword is particularly useful when
you're not sure of the exact casing of the string you're trying to match or when you
want to match multiple variations of a word or phrase. It allows you to write more
flexible and robust regular expressions that can handle different input formats. For
example, the regular expression /python/ would only match strings that contain the
word "python" in lowercase. However, if you add (?i) to the beginning of the
expression, like /(?i)python/, it will match strings that contain "python" in any
combination of uppercase and lowercase letters, such as "Python", "PYTHON", or
"pYThOn".
52. By OM PRAMOD
51
The Python Regex Handbook: Your Ultimate Cheatsheet
If you want to match parentheses as literal characters in a string, you need to escape
them with a backslash . So, if you want to match a string that contains parentheses,
you would use ( and ) in your regular expression.
Backslash
In regular expressions, the backslash () is used as an escape character. It allows you
to match special characters that have a specific meaning in regular expressions, such
as . (dot), * (asterisk), + (plus), ? (question mark), ^ (caret), $ (dollar sign), {} (curly
braces), [] (square brackets), () (parentheses), and | (pipe). For instance, if you want
to match a literal asterisk (*), you need to escape it using a backslash (). For example,
the regular expression * will match a single asterisk. Here are some examples:
53. By OM PRAMOD
52
The Python Regex Handbook: Your Ultimate Cheatsheet
A In regular expressions, the sequence A is a special sequence that matches the start of the string.
It is equivalent to the caret (^) character, which also matches the start of the string. However,
unlike the caret, A does not match the start of a line in a multiline string, it only matches the start
of the entire string.
Z In regular expressions, the sequence Z is a special sequence that matches the end of the string.
It is equivalent to the dollar sign ($) character, which also matches the end of the string. However,
unlike the dollar sign, Z does not match the end of a line in a multiline string, it only matches the
end of the entire string.
b In regular expressions, the sequence b is a special sequence that matches the empty string, but
only at the beginning or end of a word. A word is a set of alphanumeric characters surrounded by
non-alphanumeric characters (such as space).
54. By OM PRAMOD
53
The Python Regex Handbook: Your Ultimate Cheatsheet
It's important to note that the b sequence always matches the empty string or boundary
between an alphanumeric character and a non-alphanumeric character. For instance, if you are
looking for the word "ssa", using a b special sequence like this "bssab" will not return a match
because non-alphanumeric characters do not border it on both sides.
B In regular expressions, the sequence B is a special sequence that matches the empty string, but
only at positions where one side is a word character and the other side is not a word character. A
word character is a character that can be used to form words. These include alphanumeric
characters (letters and digits) and the underscore character.
55. By OM PRAMOD
54
The Python Regex Handbook: Your Ultimate Cheatsheet
In regular expressions, the sequence b is a special sequence that matches the empty string, but
only at the beginning or end of a word. A word is a set of alphanumeric characters surrounded by
non-alphanumeric characters (such as space).
d In regular expressions, the sequence d is a special sequence that matches any digit from 0 to 9.
It's equivalent to the character set [0-9].
56. By OM PRAMOD
55
The Python Regex Handbook: Your Ultimate Cheatsheet
If you want to match a specific number of digits, you can use the d sequence with a quantifier.
For example, the regular expression d{3} will match any sequence of exactly three digits.
D In regular expressions, the sequence D is a special sequence that matches any character that is
not a digit. It's equivalent to the character set [^0-9].
If you want to match a specific number of non-digit characters, you can use the D sequence with
a quantifier. For example, the regular expression D{3} will match any sequence of exactly three
non-digit characters.
57. By OM PRAMOD
56
The Python Regex Handbook: Your Ultimate Cheatsheet
s In regular expressions, the sequence s is a special sequence that matches any whitespace
character. This includes spaces, tabs, line breaks, and other space-like characters.
S In regular expressions, the sequence S is a special sequence that matches any non-whitespace
character. This includes all characters that are not spaces, tabs, line breaks, and other space-like
characters.
If you want to match a specific number of non-whitespace characters, you can use the S
sequence with a quantifier. For example, the regular expression S{3} will match any sequence of
exactly three non-whitespace characters
58. By OM PRAMOD
57
The Python Regex Handbook: Your Ultimate Cheatsheet
w In regular expressions, the sequence w is a special sequence that matches any word character.
A word character is a character that can be used to form words. These include alphanumeric
characters (letters and digits) and the underscore character.
If you want to match a specific number of word characters, you can use the w sequence with a
quantifier. For example, the regular expression w{3} will match any sequence of exactly three
word characters.
59. By OM PRAMOD
58
The Python Regex Handbook: Your Ultimate Cheatsheet
W In regular expressions, the sequence W is a special sequence that matches any non-word
character. A non-word character is a character that cannot be used to form words. These include
all characters that are not alphanumeric (letters and digits) and not underscores.
60. By OM PRAMOD
59
The Python Regex Handbook: Your Ultimate Cheatsheet
Python RegEx Expressions Functions
re.search(pattern,
string)
61. By OM PRAMOD
60
The Python Regex Handbook: Your Ultimate Cheatsheet
re.findall(pattern,
string)
62. By OM PRAMOD
61
The Python Regex Handbook: Your Ultimate Cheatsheet
re.match(pattern,
string)
63. By OM PRAMOD
62
The Python Regex Handbook: Your Ultimate Cheatsheet
re.split(pattern,
string)
64. By OM PRAMOD
63
The Python Regex Handbook: Your Ultimate Cheatsheet
re.fullmatch(pattern,
string)
65. By OM PRAMOD
64
The Python Regex Handbook: Your Ultimate Cheatsheet
re.finditer(pattern,
string)
66. By OM PRAMOD
65
The Python Regex Handbook: Your Ultimate Cheatsheet
re.sub(pattern,
replacement, string)
67. By OM PRAMOD
66
The Python Regex Handbook: Your Ultimate Cheatsheet
re.escape(string)
68. By OM PRAMOD
67
The Python Regex Handbook: Your Ultimate Cheatsheet
re.subn()
69. By OM PRAMOD
68
The Python Regex Handbook: Your Ultimate Cheatsheet
re.VERBOSE
70. By OM PRAMOD
69
The Python Regex Handbook: Your Ultimate Cheatsheet
re.MULTILINE