Regex Python cheat sheet

Lisa 49 Published: 11/26/2024

Regex Python cheat sheet

Here's a comprehensive regex Python cheat sheet for you:

Pattern Syntax

Python uses the re module to work with regular expressions. The basic syntax is as follows:

import re

pattern = r"regex_pattern"

string = "search_string"

match = re.search(pattern, string)

if match:

print(match.group()) # prints the matched text

else:

print("No match found")

Special Characters

The following special characters have specific meanings:

. (dot) matches any single character except a newline ^ matches the start of the line $ matches the end of the line | (pipe) alternation operator ( and ) group parentheses [ and ] set brackets { and } quantifier brackets escape character

Quantifiers

The following quantifiers can be used to specify how many times a pattern should match:

* (star) matches 0 or more occurrences + (plus) matches 1 or more occurrences ? (question mark) makes the preceding pattern optional (i.e., match 0 or 1 time) {n} exact number of occurrences {n,} minimum number of occurrences {n,m} range of occurrences

Character Classes

The following are examples of character classes:

[abc] matches any single character that is a, b, or c [^abc] matches any single character that is not a, b, or c [0-9] matches any digit (0 through 9) w matches any word character (alphanumeric plus underscore) W matches any non-word character d matches any digit D matches any non-digit

Predefined Sequences

The following are examples of predefined sequences:

A match only at the start of the string Z match only at the end of the string b match a word boundary (between a word character and a non-word character) B match a non-word boundary

Flags

The following are examples of flags:

re.I or (?i) ignore case re.M or (?m) multiline mode re.S or (?s) dotall mode (dot matches any character, including newline)

Examples

Here are some examples of how you can use regex in Python:

import re
Match email addresses

pattern = r"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-z]{2,}"

string = "[email protected]"

match = re.search(pattern, string)

if match:

print(match.group()) # prints "[email protected]"

Replace all occurrences of a word

pattern = r"bwordb"

string = "This is the WORD and also the world."

replacement = "phrase"

new_string = re.sub(pattern, replacement, string)

print(new_string) # prints "This is the phrase and also the phrase."

Split a string into substrings separated by a delimiter

pattern = r"s+"

string = "hello,world,python"

substrings = re.split(pattern, string)

print(substrings) # prints ["hello", "world", "python"]

I hope this helps!

Python re module w3schools example

Here is the rewritten text in English:

The re module in Python provides support for regular expressions (regex). Regular expressions are a way to describe patterns in strings, and can be used for tasks such as searching, validating, and extracting data from strings.

Here's an example from W3Schools that demonstrates how to use the re module:

import re
Pattern

pattern = r"hellos+world"

String to search

text = "hello world"

Search for the pattern in the text

match = re.search(pattern, text)

if match:

print("Match found: ", match.group())

else:

print("No match")

In this example:

We import the re module. We define a regular expression pattern (r"hellos+world"). This pattern matches the string "hello followed by one or more whitespace characters and then world". We create a text variable with the value "hello world", which contains the text we want to search for the pattern. We use the re.search() function to find all non-overlapping matches of the pattern in the text. The search() method scans through the text looking for a match, and returns a match object if it finds one.

If you run this code, the output will be:

Match found:  hello  world

This demonstrates how to use regular expressions with the re module in Python.

The regular expression pattern (r"hellos+world"):

"hello" matches the literal string "hello". s+ matches one or more whitespace characters (spaces, tabs, etc.). "world" matches the literal string "world".

In this example, the regular expression is used to search for a specific pattern in a text string. The re module provides several methods for working with regular expressions, including search(), match(), and findall().

Here's a breakdown of the different parts of the code:

import re: This line imports the re (regular expression) module. pattern = r"hellos+world": This line defines the regular expression pattern that we want to search for in our text. The r prefix means this is a raw string, and won't be interpreted as a Python string. text = "hello world": This line creates a text variable with the value "hello world". This is the text that we'll use to test our pattern. match = re.search(pattern, text) : This line searches for our regular expression pattern in our text. If it finds a match, it returns a match object; otherwise, it returns None. if match: ... else: ...: These lines check whether the re.search() method found a match or not.

If you're interested in learning more about regular expressions and how to use them with Python's re module, I'd be happy to help!