regular expression

A pattern language for matching text, where characters such as ., *, ^ and $ have special meanings.

In more detail

A dot matches any single character, a star repeats the item before it, ^ anchors to the start of a line and $ to the end, and square brackets match one character from a set. Tools such as grep, sed and awk use them, as do most programming languages, with small differences in syntax. Escape a special character with a backslash when you mean it literally.

Examples

grep -F '4.50' prices.txt
grep '4\.50' prices.txt
[[ $line =~ ^[0-9]+$ ]]
The token resolver uses ^\{([^}]+)\}$ to recognise an alias value and capture the token path inside the braces.
Structured logs avoid regular-expression parsing of log messages.
git log --grep '^feat'
git config --get-regexp '^alias'
grep -Eq "^(feat|fix): .+"
A native Node.js router can match request paths with a pattern such as /^\/users\/(\d+)$/ and pull out the ID.
Pulling an amount out of prose logs needs a regular expression that silently breaks when the wording changes.
PromQL regex matcher: http_requests_total{route=~"/api/.*"}
import re
re.findall(r"\d+", "3 cats, 12 dogs")   # ["3", "12"]
Regular expressions are the right tool for free-text logs such as sshd lines; for JSON, walk the parsed structure instead.
PostgreSQL: WHERE name ~* 'cable' (~* ignores case). SQLite has a REGEXP operator but no built-in implementation.
WHERE name ~* 'cable'
In this course: pytest.raises takes a match argument that is a regular expression searched for in the error message, so a test can check that the right error was raised for the right reason. For example, match="no rate for" passes if that text appears anywhere in the error message.
with pytest.raises(KeyError, match="MOON"):

Related terms

Taught in

References

Certification courses