December 13, 2024
O. Wolfson
Regular expressions often seem intimidating at first glance. To make regex more accessible and actionable, I plan to write practical blog series that will walk us through real-world use cases. From matching email addresses to validating passwords, each post will focus on a specific task, providing step-by-step guidance and allowing us to test our newfound skills interactively here or on a regex test sitelike Regexr.
In this series, I'll cover everything from beginner-friendly patterns to advanced text manipulations, helping us master regex one task at a time. Use the Regex Tester to test your regex against a test string. Matches are highlighted in yellow.
Example Regex: \b(apple|banana)\b
Example String:
textI have an apple, a banana, and 34 oranges. Today, I bought 12 more apples and sent an email to fruitlover@example.com to share my joy. The date was 2024-12-16, but I also noted it down as 16/12/2024 for my journal. "Fresh fruits are amazing," I thought to myself. I called my friend at +1-800-555-0199 and shared the news, as well as a story about how jogging in the morning helps me stay healthy. She replied with a hashtag: #FruitForLife, and sent me a link to a great website: https://www.healthyliving.com.
Match Specific Words or Phrases
Task: Match occurrences of "apple" or "banana" in a text.
regex\b(apple|banana)\b
Find Digits in Text
Task: Extract all numbers from "I have 12 apples and 34 oranges."
regex\d+
Locate Emails
Task: Identify valid email addresses in a block of text.
regex[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}
Match Dates
Task: Match dates in formats like YYYY-MM-DD
or DD/MM/YYYY
.
regex\b\d{4}-\d{2}-\d{2}\b
or
regex\b\d{2}/\d{2}/\d{4}\b
Extract Words Between Quotation Marks
Task: Capture content within double quotes.
regex"([^"]+)"
Phone Number Validation
Task: Match phone numbers in different formats (e.g., +1-800-555-0199
, (123) 456-7890
).
regex\+?\d{1,3}?[-.\s]?\(?\d{1,4}?\)?[-.\s]?\d{1,4}[-.\s]?\d{1,9}
Find Words Ending in Specific Letters
Task: Identify words ending with "ing".
regex\b\w+ing\b
Validate URLs
Task: Match valid URLs in a text.
regexhttps?:\/\/[^\s/$.?#].[^\s]*
Extract Hashtags
Task: Find hashtags in a block of social media posts.
regex#\w+
Match Nested HTML Tags
Task: Capture content inside <div>
tags.
regex<div>(.*?)<\/div>
Highlight Incorrect Capitalization
Task: Identify sentences that do not start with an uppercase letter.
regex(?<![.?!]\s)[a-z]
Extract CSV Columns
Task: Match and extract values in a CSV file.
regex(?:^|,)([^,]*)
Filter Log Entries
Task: Identify error messages in a system log.
regexERROR:\s.*
Match IP Addresses
Task: Find valid IPv4 addresses.
regex\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b
Search and Replace
Task: Replace all occurrences of "foo" with "bar".
regexfoo
Replacement:
textbar
Password Validation
Task: Match strong passwords (e.g., at least 8 characters, one uppercase letter, one digit).
regex(?=.*[A-Z])(?=.*\d)[A-Za-z\d]{8,}
Clean Up Whitespace
Task: Remove extra spaces from a text.
regex\s{2,}
Replacement:
text(a single space)
Extract Domain Names
Task: Capture the domain part of an email address.
regex@([a-zA-Z0-9.-]+\.[a-zA-Z]{2,})
Find Palindromes
Task: Match palindromic words (e.g., "madam", "racecar").
regex\b(\w)(\w)?\w?\2\1\b
Scrape Markdown Links
Task: Extract links from Markdown syntax [text](url)
.
regex\[(.*?)\]\((.*?)\)
Please like this post if you want to see this series continue!