Skip to content
UPEEC

Regular Expressions

The re module finds and extracts text patterns.

Intermediate
Comments 0
import re
re.findall(r"\d+", "R1=220, R2=4700")          # ['1', '220', '2', '4700']
m = re.match(r"(\w+)=(\d+)", "R1=220")
m.group(1), m.group(2)                          # ('R1', '220')
re.sub(r"\s+", " ", text)                       # collapse whitespace

Use raw strings r"...", compile reused patterns with re.compile, and prefer simple string methods when they suffice.

Spotted a mistake or want to add detail? Suggest an improvement — Content Contributors' edits are reviewed by editors before they go live.

Discussion (0)

Sign in or join free to ask questions and discuss this topic.