Menu
Coddy logo textTech

Escape Sequences

Lesson 23 of 28 in Coddy's RegEx in Python course.

Escape sequences allow you to include special characters in your regex patterns. Here are some common escape sequences:

  • \d: Matches any digit (equivalent to [0-9]).
  • \D: Matches any non-digit character (equivalent to [^0-9]).
  • \w: Matches any word character (equivalent to [a-zA-Z0-9_]).
  • \W: Matches any non-word character (equivalent to [^a-zA-Z0-9_]).
  • \s: Matches any whitespace character (equivalent to [ \t\n\r\f\v]).
  • \S: Matches any non-whitespace character (equivalent to [^ \t\n\r\f\v]).
import re

text = "The price is $5.99 on 06/12/2024."
pattern = r"\d{2}/\d{2}/\d{4}"
matches = re.findall(pattern, text)

print(matches)  # Output: ['06/12/2024']

In the above example, the pattern \d{2}/\d{2}/\d{4} matches a date in the format "MM/DD/YYYY". The \d escape sequence matches any digit.

Try it yourself

This lesson doesn't include a code challenge.

All lessons in RegEx in Python