
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How to use special characters in Python Regular Expression?
From Python documentation
Non-special characters match themselves. Special characters don't match themselves −
\ | Escape special char or start a sequence. |
. | Match any char except newline, see re.DOTALL |
^ | Match start of the string, see re.MULTILINE |
$ | Match end of the string, see re.MULTILINE |
[ ] | Enclose a set of matchable chars |
R|S | Match either regex R or regex S. |
() | Create capture group, & indicate precedence |
After '[', enclose a set, the only special chars are −
] | End the set, if not the 1st char |
- | A range, eg. a-c matches a, b or c |
^ | Negate the set only if it is the 1st char |
Quantifiers (append '?' for non-greedy) −
{m} | Exactly m repetitions |
{m,n} | From m (default 0) to n (default infinity) |
* | 0 or more. Same as {,} |
+ | 1 or more. Same as {1,} |
? | 0 or 1. Same as {,1} |
- Related Articles
- How to use wildcard in Python regular expression?
- How to use range in Python regular expression?
- How to use variables in Python regular expression?
- How to write Python regular expression to check alphanumeric characters?
- Write a Regular Expression to remove all special characters from a JavaScript String?
- Regular Expression Special Variables in Perl
- How to create regular expression only accept special formula?
- How to write a Python regular expression to use re.findall()?
- How to split on successions of newline characters using Python regular expression?
- How to use Unicode and Special Characters in Tkinter?
- MySQL regular expression to update a table with column values including string, numbers and special characters
- How do we use re.finditer() method in Python regular expression?
- How to match nonword characters in Python using Regular Expression?\n\n\n\n
- How do we use Python Regular Expression named groups?
- How to use special characters in column names with MySQL?

Advertisements