Regex
We need to understand a bit about regexes, or "regular expressions". "Regex" for short, is a programming-language-agnostic way of searching for patterns in text.
Last updated
We need to understand a bit about regexes, or "regular expressions". "Regex" for short, is a programming-language-agnostic way of searching for patterns in text.
Last updated
Related Sites:
To get really good at using regex, we'd need a full course on the topic. For now, let's just cover the basics. In Python, we can use the to work with regex. It has a function that will return a list of all the matches in a string. See examples below.
\d
matches any digit
{3}
means "exactly three of the preceding character"
-
is just a literal -
that we want to match
\(
and \)
are escaped parentheses that we want to match
+
means "one or more of the preceding character"
@
is just a literal @
symbol that we want to match
\.
is a literal .
that we want to match (The .
is a special character in regex, so we escape it with a leading backslash)
(
and )
is a , meaning it groups the matched text, allowing us to reference or extract it separately.
.*?
matches any number of characters (except for ) between the parentheses
\w
matches any word character ( characters and underscores)
The function that will return a list of all the matches in a string.
Use for interactive regex testing, it breaks down each part of the pattern and explains what it does.