2024-12-16 Web Development, Productivity, Programming
Master Regex: A Practical Series with an Interactive Tester
By 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:
Regex Tester
Beginner Level: Simple Patterns
-
Match Specific Words or Phrases
-
Task: Match occurrences of "apple" or "banana" in a text.
-
-
Find Digits in Text
-
Task: Extract all numbers from "I have 12 apples and 34 oranges."
-
-
Locate Emails
-
Task: Identify valid email addresses in a block of text.
-
Intermediate Level: Grouping and Quantifiers
-
Match Dates
-
Task: Match dates in formats like
YYYY-MM-DD
orDD/MM/YYYY
.or
-
-
Extract Words Between Quotation Marks
-
Task: Capture content within double quotes.
-
-
Phone Number Validation
-
Task: Match phone numbers in different formats (e.g.,
+1-800-555-0199
,(123) 456-7890
).
-
-
Find Words Ending in Specific Letters
-
Task: Identify words ending with "ing".
-
Advanced Level: Advanced Patterns
-
Validate URLs
-
Task: Match valid URLs in a text.
-
-
Extract Hashtags
-
Task: Find hashtags in a block of social media posts.
-
-
Match Nested HTML Tags
-
Task: Capture content inside
<div>
tags.
-
-
Highlight Incorrect Capitalization
-
Task: Identify sentences that do not start with an uppercase letter.
-
Real-World Applications
-
Extract CSV Columns
-
Task: Match and extract values in a CSV file.
-
-
Filter Log Entries
-
Task: Identify error messages in a system log.
-
-
Match IP Addresses
-
Task: Find valid IPv4 addresses.
-
-
Search and Replace
-
Task: Replace all occurrences of "foo" with "bar".
Replacement:
-
Bonus Challenges
-
Password Validation
-
Task: Match strong passwords (e.g., at least 8 characters, one uppercase letter, one digit).
-
-
Clean Up Whitespace
-
Task: Remove extra spaces from a text.
Replacement:
-
-
Extract Domain Names
-
Task: Capture the domain part of an email address.
-
-
Find Palindromes
-
Task: Match palindromic words (e.g., "madam", "racecar").
-
-
Scrape Markdown Links
-
Task: Extract links from Markdown syntax
[text](url)
.
-
Please like this post if you want to see this series continue!