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:

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

Regex Tester

Beginner Level: Simple Patterns

  1. Match Specific Words or Phrases

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

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

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

      regex
      \d+
      
  3. 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,}
      

Intermediate Level: Grouping and Quantifiers

  1. 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
      
  2. Extract Words Between Quotation Marks

    • Task: Capture content within double quotes.

      regex
      "([^"]+)"
      
  3. 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}
      
  4. Find Words Ending in Specific Letters

    • Task: Identify words ending with "ing".

      regex
      \b\w+ing\b
      

Advanced Level: Advanced Patterns

  1. Validate URLs

    • Task: Match valid URLs in a text.

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

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

      regex
      #\w+
      
  3. Match Nested HTML Tags

    • Task: Capture content inside <div> tags.

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

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

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

Real-World Applications

  1. Extract CSV Columns

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

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

    • Task: Identify error messages in a system log.

      regex
      ERROR:\s.*
      
  3. Match IP Addresses

    • Task: Find valid IPv4 addresses.

      regex
      \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".

      regex
      foo
      

      Replacement:

      text
      bar
      

Bonus Challenges

  1. 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,}
      
  2. Clean Up Whitespace

    • Task: Remove extra spaces from a text.

      regex
      \s{2,}
      

      Replacement:

      text
      (a single space)
      
  3. Extract Domain Names

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

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

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

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

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

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

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