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

Now let us see how backslashes work 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 (r'') to simplify regex patterns. This prevents Python from treating backslashes as escape characters.

There are several ways to see how backslashes work in Python regular expressions using the examples, such as -

Example: Escaping Special Characters

The following example will search for a dollar sign ($) within a given text string. As '$' is a special character in regex, we will use a backslash (\) to escape it, and make sure it is treated as a normal character. The re.search() function scans the text and returns the first matching occurrence.

# Import re module
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")

Output

This will create the following outcome -

$

Example: Matching Digits Using \d

This example shows how to extract numbers from a text string with the help of '\d+'. The regex pattern '\d+' matches one or more consecutive digits, which allows us to find numerical values easily. The re.search() function returns the first match found.

# Import regular expressions module
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")

Output

This will generate the following result -

5678

Example: Using \s to Match Whitespace

Here, we will find spaces between words in a string with the help of the '\s+' pattern. The regex sequence '\s' represents whitespace characters, and the "+" quantifier makes sure we capture one or more spaces. And the re.search() function will help us to locate the matching spaces.

# Import regular expressions module
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 result of the search
print("Match" if match else "No match")

Output

This will produce the following result -

# Whitespaces will be displayed as a single space

Example: Extracting Words With Boundaries

This example will search for the word "python" in the given sentence to make sure it appears as a simple word. The '\b' boundary markers prevent partial matches by ensuring the word is not part of a longer term like "pythonic." The re.search() function will return the exact word match.

# Import regular expressions module
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")

Output

This will lead to the following outcome -

python
Updated on: 2025-05-26T17:15:27+05:30

631 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements