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
 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) −

Exactly m repetitions
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}


Updated on: 13-Jun-2020

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started