Special characters (like the character class brackets [
and ]
below) are not matched literally:
match = re.search(r'[b]', 'a[b]c')
match.group()
# Out: 'b'
By escaping the special characters, they can be matched literally:
match = re.search(r'\[b\]', 'a[b]c')
match.group()
# Out: '[b]'
The re.escape()
function can be used to do this for you:
re.escape('a[b]c')
# Out: 'a\\[b\\]c'
match = re.search(re.escape('a[b]c'), 'a[b]c')
match.group()
# Out: 'a[b]c'
The re.escape()
function escapes all special characters, so it is useful if you are composing a regular expression based on user input:
username = 'A.C.' # suppose this came from the user
re.findall(r'Hi {}!'.format(username), 'Hi A.C.! Hi ABCD!')
# Out: ['Hi A.C.!', 'Hi ABCD!']
re.findall(r'Hi {}!'.format(re.escape(username)), 'Hi A.C.! Hi ABCD!')
# Out: ['Hi A.C.!']