How do backslashes work in Python Regular Expressions?

Backslashes in Python regular expressions (regex) are used to define special sequences and escape characters. As backslashes are also used in Python strings, we need to be careful while using them.

How Backslashes Work

Here's how backslashes function in Python regular expressions −

  • Escaping Characters: Some characters have specific meanings in regex (such as . or *). To treat them as normal characters, use a backslash (\.).

  • Special Sequences: Special sequences in regex include \d (digits) and \s (whitespace), where backslashes represent special meanings.

  • Raw Strings (r''): Python allows you to use raw strings to simplify regex patterns. This prevents Python from treating backslashes as escape characters.

Let's explore these concepts through practical examples −

Example: Escaping Special Characters

The following example searches for a dollar sign ($) within a text string. Since '$' is a special character in regex, we use a backslash (\) to escape it ?

import re

# Define a string to search
txt = "Price is 100$"

# Search for the pattern "$" in the string
pattern = r"\$"  

# Use re.search to find the pattern in the string
match = re.search(pattern, txt)

# Print the matched string or a message if no match is found
print(match.group() if match else "No match")

The output of the above code is −

$

Example: Matching Digits Using \d

This example shows how to extract numbers from a text string using \d+. The pattern matches one or more consecutive digits ?

import re

# Define a string to search
txt = "Order ID: 5678"

# Search for a pattern in the string
pattern = r"\d+"  

# Find the first occurrence of the pattern in the string
match = re.search(pattern, txt)

# Print the matched string or a message if no match is found
print(match.group() if match else "No match")

The output of the above code is −

5678

Example: Using \s to Match Whitespace

Here, we find spaces between words using the \s+ pattern. The sequence \s represents whitespace characters, and the "+" quantifier captures one or more spaces ?

import re

# Define a string to search
txt = "Hello  World"

# Search for a pattern in the string
pattern = r"\s+"  

# Use re.search to find the pattern in the string
match = re.search(pattern, txt)

# Print the matched whitespace
print(repr(match.group()) if match else "No match")

The output of the above code is −

'  '

Example: Extracting Words With Boundaries

This example searches for the word "python" using word boundaries. The \b markers ensure the word appears as a complete word, not as part of a longer term ?

import re

# Define a string to search
txt = "python rocks!"

# Search for the word 'python' in the string
pattern = r"\bpython\b"  

# Use re.search to find the pattern in the string
match = re.search(pattern, txt)

# Print the matched string 
print(match.group() if match else "No match")

The output of the above code is −

python

Raw Strings vs Regular Strings

When working with backslashes in regex, raw strings help avoid double escaping ?

import re

text = "file\path\name.txt"

# Without raw string (needs double backslash)
pattern1 = "\\path"

# With raw string (single backslash)
pattern2 = r"\path"

match1 = re.search(pattern1, text)
match2 = re.search(pattern2, text)

print("Without raw string:", match1.group() if match1 else "No match")
print("With raw string:", match2.group() if match2 else "No match")

The output of the above code is −

Without raw string: \path
With raw string: \path

Conclusion

Backslashes in Python regex serve two main purposes: escaping special characters and creating special sequences like \d and \s. Using raw strings (r'') simplifies regex patterns by preventing Python from interpreting backslashes as escape characters.

Updated on: 2026-03-24T19:13:53+05:30

838 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements