Regex Cheat Sheet
Regex Syntax
Character |
Matches |
---|---|
|
|
|
Any character (except newline) |
|
|
|
|
|
|
Matches |
Description |
|
---|---|---|
|
Any one of the letters |
Set of characters |
|
Any character but |
Complement of a set of characters |
|
Any one of the letters |
Range of characters |
|
Any of |
Range of characters |
Type |
Expression |
Equivalent To |
Description |
---|---|---|---|
Word Character |
|
|
Alphanumeric or underscore |
Non-word Character |
|
|
Anything but a word character |
Digit Character |
|
|
Numeric |
Non-digit Character |
|
|
Non-numeric |
Whitespace Character |
|
|
Whitespace |
Non-whitespace Character |
|
|
Anything but a whitespace character |
Anchor |
Matches |
---|---|
|
Start of the string |
|
End of the string |
|
Boundary between word and non-word characters |
Group Type |
Expression |
---|---|
Capturing |
|
Non-capturing |
|
Quantifier |
Modification |
---|---|
|
Match expression exactly 5 times |
|
Match expression 2 to 5 times |
|
Match expression 2 or more times |
|
Match expression 0 to 5 times |
|
Match expression 0 or more times |
|
Match expression 0 or more times |
|
Match expression 0 or 1 times |
|
Match expression 0 or 1 times |
|
Match expression 1 or more times |
|
Match expression 1 or more times |
Quantifier |
Modification |
---|---|
|
Match 2 to 5 times (less preferred) |
|
Match 2 or more times (less preferred) |
|
Match 0 to 5 times (less preferred) |
|
Match 0 or more times (less preferred) |
|
Match 0 or more times (less preferred) |
|
Match 0 or 1 times (less preferred) |
|
Match 0 or 1 times (less preferred) |
|
Match 1 or more times (less preferred) |
|
Match 1 or more times (less preferred) |
Quantifier |
Modification |
---|---|
|
Match string |
Quantifier |
Modification |
---|---|
|
Zero-width match confirming |
|
Zero-width match confirming |
Python
Function |
Purpose |
Usage |
---|---|---|
|
Return a match object if pattern found in string |
|
|
Return an iterable of match objects (one for each match) |
|
|
Return a list of all matched strings (different when capture groups) |
|
|
Split string by regex delimeter & return string list |
|
|
Compile a regular expression pattern for later use |
|
Flag |
Description |
---|---|
|
Match uppercase and lowercase characters interchangeably |
|
Ignore whitespace characters and allow |
I send out 1 Python exercise every week through a Python skill-building service called Python Morsels.
If you'd like to improve your Python skills every week, sign up!
You can find the Privacy Policy here.reCAPTCHA protected (Google Privacy Policy & TOS)