Chapter 3: Simple Use Cases
Introduction
This chapter shows how to use regular expressions for everyday tasks. Each example is a simple solution to a common problem.
Basic pattern matching
Example 1: Finding a word
To find a specific word in a text, search for the word itself:
word
This pattern matches the exact word “word” in the text.
It also matches those four letters inside longer words, such as the “word” in “wordy”. To match only the whole word, put the word boundary \b on each side: \bword\b. The section on finding words with a specific prefix, below, explains \b.
Example 2: Case-insensitive search
To perform a case-insensitive search, turn on the i flag. Writing (?i) at the start of the pattern does that:
(?i)word
This matches “Word”, “word”, “WORD”, etc. Most languages can also set the flag outside the pattern: /word/i in JavaScript, re.IGNORECASE in Python.
Searching for text
Example 1: Finding all digits
To find all digits in a text:
\d+
This pattern matches sequences of digits like “123”, “456”, etc.
Example 2: Finding words with specific prefix
To find words starting with “pre”:
\bpre\w*\b
This matches “prefix”, “prelude”, etc.
The \b in the regex \bpre\w*\b is a word boundary anchor. It makes the pattern match only at the beginning and end of a word. Here’s how each part, including \b, works in this pattern:
- \b: Asserts a word boundary at the start.
- pre: Matches the literal string “pre”.
- \w*: Matches zero or more word characters (letters, digits, or underscores).
- \b: Asserts a word boundary at the end.
Because of \b, “pre” matches only at the beginning of a word and never inside one, so the pattern finds “prefix” but not “depression”.
Validating input
Example 1: Email address validation
To validate an email address:
^[\w\.-]+@[a-zA-Z\d\.-]+\.[a-zA-Z]{2,6}$
This pattern checks for a valid email format.
Here is the pattern, piece by piece:
^: Asserts the position at the start of the string.[\w\.-]+:
[and]: Define a character class.\w: Matches any word character (equivalent to [a-zA-Z0-9_]).\.: Matches a literal dot (.). The backslash is used to escape the dot, as a dot normally matches any character.-: Matches a literal hyphen (-).+: Quantifier that matches one or more occurrences of the preceding element (in this case, any word character, dot, or hyphen).
Together, this part matches one or more word characters, dots, or hyphens: the local part of the email address (the part before the @).
@: Matches the literal “@” character.[a-zA-Z\d\.-]+:
[and]: Define a character class.a-zA-Z: Matches any uppercase or lowercase letter.\d: Matches any digit (equivalent to [0-9]).\.: Matches a literal dot (.).-: Matches a literal hyphen (-).+: Quantifier that matches one or more occurrences of the preceding element (in this case, any letter, digit, dot, or hyphen).
Together, this part matches one or more letters, digits, dots, or hyphens: the domain part of the email address (the part after the @).
\.: Matches a literal dot (.), separating the domain name and the top-level domain (TLD).[a-zA-Z]{2,6}:
[and]: Define a character class.a-zA-Z: Matches any uppercase or lowercase letter.{2,6}: Quantifier that matches between 2 and 6 occurrences of the preceding element (in this case, any letter).
Together, this part matches a top-level domain (TLD) of 2 to 6 letters, such as “com”, “org”, “net”, or “info”.
$: Asserts the position at the end of the string.
Example 2: Phone number validation
To validate a US phone number:
^\(\d{3}\) \d{3}-\d{4}$
This matches phone numbers in the format “(123) 456-7890”.
Replacing text
Example 1: Replacing all spaces with underscores
To replace spaces with underscores, match each run of whitespace:
\s+
and replace every match with _. “my holiday photo.jpg” becomes “my_holiday_photo.jpg”.
Example 2: Removing punctuation
To remove all punctuation:
[^\w\s]+
Replacing each match with nothing removes all the punctuation marks.
Practical examples in everyday tasks
Example 1: Extracting URLs from text
To extract URLs from a text:
https?://[^\s]+
This pattern matches “http://” or “https://” followed by non-space characters.
Example 2: Finding dates in text
To find dates in the format “dd/mm/yyyy”:
\d{2}/\d{2}/\d{4}
This matches dates like “23/07/2024”.
Example 3: Identifying capitalized words
To find all capitalized words:
\b[A-Z][a-z]*\b
This matches words starting with a capital letter.