
- 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 do backslashes work in Python Regular Expressions?
According to Python docs, perhaps the most important metacharacter in regular expressions is the backslash, \. As in Python string literals, the backslash can be followed by various characters to indicate various special sequences. It’s also used to escape all the metacharacters so you can still match them in patterns; for example, if you need to match a [ or \, you can precede them with a backslash to remove their special meaning: \[ or \.
The following code highlights the function of backslash in Python regex
Example
import re result = re.search('\d', '\d') print result result = re.search(r'\d', '\d') print result.group()
Output
This gives the output
None \d
- Related Articles
- How do you use regular expressions in Cucumber?
- Why do backslashes appear twice while printing in Python?
- How regular expression modifiers work in Python?
- How regular expression alternatives work in Python?
- How regular expression anchors work in Python?
- How to compare regular expressions in Perl and Python?
- How to match whitespace in python using regular expressions
- How does [d+] regular expression work in Python?
- How does B regular expression work in Python?
- How to use regular expressions in css in Selenium with python?
- How to use regular expressions in xpath in Selenium with python?
- Extracting email addresses using regular expressions in Python
- How do we use wildcard or regular expressions with a jQuery selector?
- How do Python modules work?
- JavaScript Regular Expressions

Advertisements