Oliver Wolfson
ServicesProjectsContact

Development Services

SaaS apps · AI systems · MVP builds · Technical consulting

Services·Blog
© 2025 O. Wolf. All rights reserved.
Web DevelopmentProductivityProgramming
Master Regex: A Practical Series with an Interactive Tester
Regular expressions, or regex, are powerful tools for solving complex text processing challenges.
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:

I 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.

Component "RegexHighlighter" is not available. Please define it in the MDX components.

Beginner Level: Simple Patterns

  1. Match Specific Words or Phrases

    • Task: Match occurrences of "apple" or "banana" in a text.

      \b(apple|banana)\b
      
  2. Find Digits in Text

    • Task: Extract all numbers from "I have 12 apples and 34 oranges."

      \d+
      
  3. Locate Emails

    • Task: Identify valid email addresses in a block of text.

      [a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}
      

Intermediate Level: Grouping and Quantifiers

  1. Match Dates

    • Task: Match dates in formats like YYYY-MM-DD or DD/MM/YYYY.

      \b\d{4}-\d{2}-\d{2}\b
      

      or

      \b\d{2}/\d{2}/\d{4}\b
      
  2. Extract Words Between Quotation Marks

    • Task: Capture content within double quotes.

      "([^"]+)"
      
  3. Phone Number Validation

    • Task: Match phone numbers in different formats (e.g., +1-800-555-0199, (123) 456-7890).

      \+?\d{1,3}?[-.\s]?\(?\d{1,4}?\)?[-.\s]?\d{1,4}[-.\s]?\d{1,9}
      
  4. Find Words Ending in Specific Letters

    • Task: Identify words ending with "ing".

      \b\w+ing\b
      

Advanced Level: Advanced Patterns

  1. Validate URLs

    • Task: Match valid URLs in a text.

      https?:\/\/[^\s/$.?#].[^\s]*
      
  2. Extract Hashtags

    • Task: Find hashtags in a block of social media posts.

      #\w+
      
  3. Match Nested HTML Tags

    • Task: Capture content inside <div> tags.

      <div>(.*?)<\/div>
      
  4. Highlight Incorrect Capitalization

    • Task: Identify sentences that do not start with an uppercase letter.

      (?<![.?!]\s)[a-z]
      

Real-World Applications

  1. Extract CSV Columns

    • Task: Match and extract values in a CSV file.

      (?:^|,)([^,]*)
      
  2. Filter Log Entries

    • Task: Identify error messages in a system log.

      ERROR:\s.*
      
  3. Match IP Addresses

    • Task: Find valid IPv4 addresses.

      \b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b
      
  4. Search and Replace

    • Task: Replace all occurrences of "foo" with "bar".

      foo
      

      Replacement:

      bar
      

Bonus Challenges

  1. Password Validation

    • Task: Match strong passwords (e.g., at least 8 characters, one uppercase letter, one digit).

      (?=.*[A-Z])(?=.*\d)[A-Za-z\d]{8,}
      
  2. Clean Up Whitespace

    • Task: Remove extra spaces from a text.

      \s{2,}
      

      Replacement:

      (a single space)
      
  3. Extract Domain Names

    • Task: Capture the domain part of an email address.

      @([a-zA-Z0-9.-]+\.[a-zA-Z]{2,})
      
  4. Find Palindromes

    • Task: Match palindromic words (e.g., "madam", "racecar").

      \b(\w)(\w)?\w?\2\1\b
      
  5. Scrape Markdown Links

    • Task: Extract links from Markdown syntax [text](url).

      \[(.*?)\]\((.*?)\)
      

Please like this post if you want to see this series continue!

Tags
#regex#core utils#regex series