Online Visual Python Regex Tester

Enter a regular expression pattern and a string to test it against.

Matches

Python Regex Cheat Sheet

Basic Patterns

  • . Matches any character except a newline
  • d Matches any digit
  • D Matches any non-digit character
  • w Matches any alphanumeric character
  • W Matches any non-alphanumeric character
  • s Matches any whitespace character
  • S Matches any non-whitespace character
  • ^ Matches the start of a string
  • $ Matches the end of a string

Quantifiers

  • * Matches zero or more occurrences
  • + Matches one or more occurrences
  • ? Matches zero or one occurrence
  • {n} Matches exactly n occurrences
  • {n,} Matches n or more occurrences
  • {n,m} Matches between n and m occurrences

Character Classes

  • [abc] Matches any character in the set (a, b, or c)
  • [^abc] Matches any character not in the set (not a, b, or c)
  • [a-z] Matches any character in the range (a to z)
  • [A-Z] Matches any uppercase character
  • [0-9] Matches any digit

Grouping and Capturing

  • ( ) Groups multiple patterns together
  • (?: ) Non-capturing group
  • (?P<name> ) Capturing group with name

Anchors

  • \b Matches a word boundary
  • \B Matches a non-word boundary
  • \A Matches the start of a string
  • \Z Matches the end of a string

Special Characters

  • \ Escapes a special character
  • . Matches any character

Regular Expressions in Python

Regular expressions are powerful tools for pattern matching and text manipulation. They allow us to search, match, and manipulate strings based on specific patterns.

In Python, you can use the re module to work with regular expressions. Here's an example that demonstrates how to use regex with capturing groups:

import re

# Match a pattern and capture groups
text = "Hello, my name is Alice. I am 25 years old."
pattern = r"Hello, my name is (\w+). I am (\d+) years old."

match = re.search(pattern, text)
if match:
    name = match.group(1)
    age = match.group(2)
    print(f"Name: {name}")
    print(f"Age: {age}")

In this code, we import the re module and define a pattern using a raw string (r"..."). The pattern includes capturing groups denoted by parentheses (...).

We use re.search() to search for the pattern within the text string. If a match is found, we can access the captured groups using the group() method on the match object. The captured groups are numbered starting from 1.

The output of the code will be:

Name: Alice
Age: 25

In this example, we match the pattern "Hello, my name is (\w+). I am (\d+) years old." against the text string. The first capturing group (\w+) captures the name, and the second capturing group (\d+) captures the age.

By using capturing groups, you can extract specific parts of a matched pattern and use them for further processing or manipulation.

Regular expressions offer a wide range of features and syntax for pattern matching, including character classes, quantifiers, anchors, and more. You can refer to the Python documentation for more information on regular expressions and their syntax.