Tutorial

Regular Expressions (Regex) from Scratch: A Comprehensive Guide

Deep dive into Regular Expressions (Regex) syntax, metacharacters, quantifiers, anchors, capturing groups, and advanced lookaround assertions. Includes common examples and an online testing tool.

What are Regular Expressions?

Regular Expressions (commonly known as Regex or Regexp) are a powerful tool for describing, matching, and manipulating strings. By using a specific combination of characters (a pattern), you can search for, validate, or replace text that fits a defined rule.

Whether you’re validating an email address or parsing complex log files, Regex can significantly boost your productivity. Almost all modern programming languages (such as JavaScript, Python, Java, C++, Go, etc.) have built-in support for regular expressions.

1. Basic Syntax

A regular expression is mainly composed of literal characters and metacharacters.

Literal Characters

Literal characters include all uppercase and lowercase letters, numbers, punctuation marks, and symbols. They represent their literal meaning when matching. For example, the pattern cat matches the exact string “cat”.

Metacharacters

Metacharacters are characters with special meanings and are the core of regular expressions.

MetacharacterDescription
.Matches any single character except for line terminators.
\Escape character. Escapes the next character or identifies special sequences.
^Matches the beginning of the string or line.
$Matches the end of the string or line.
*Matches the preceding subexpression zero or more times.
+Matches the preceding subexpression one or more times.
?Matches the preceding subexpression zero or one time.
|Logical “OR” operator.
()Marks the beginning and end of a subexpression for grouping.
[]Marks a bracketed expression used to match any single character within it.
{}Marks a quantifier expression.

2. Character Classes

Character classes allow you to match any one of a set of characters.

  • [abc]: Matches “a”, “b”, or “c”.
  • [^abc]: Matches any character except “a”, “b”, or “c”.
  • [a-z]: Matches any lowercase letter from a to z.
  • [A-Z]: Matches any uppercase letter from A to Z.
  • [0-9]: Matches any digit from 0 to 9.

Common Shorthands:

  • \d: Matches a digit, equivalent to [0-9].
  • \D: Matches a non-digit, equivalent to [^0-9].
  • \w: Matches any alphanumeric character (including underscores), equivalent to [a-zA-Z0-9_].
  • \W: Matches any non-word character.
  • \s: Matches any whitespace character (space, tab, newline, etc.).
  • \S: Matches any non-whitespace character.

3. Quantifiers

Quantifiers specify the number of times a pattern should occur.

  • *: 0 or more times.
  • +: 1 or more times.
  • ?: 0 or 1 time (often used for optional items).
  • {n}: Exactly n times.
  • {n,}: At least n times.
  • {n,m}: Between n and m times.

Greedy vs. Lazy Matching:

  • Greedy: By default, Regex is “greedy,” meaning it matches as much text as possible. For example, a.*b matching axxxbxxxb will match the entire string.
  • Lazy (Non-greedy): Adding a ? after a quantifier (e.g., *?, +?) makes it match the shortest possible string. For example, a.*?b matching axxxbxxxb will only match the first axxxb.

4. Anchors (Boundary Matchers)

Anchors do not match actual characters; they match “positions” within the string.

  • ^: Matches the beginning of the string (or line in multiline mode).
  • $: Matches the end of the string (or line in multiline mode).
  • \b: Matches a word boundary. The position between a word character (\w) and a non-word character.
  • \B: Matches a position that is not a word boundary.

5. Grouping & Capturing

  • (expression): Capturing group. Matches and remembers the content for later use via backreferences like \1, \2.
  • (?:expression): Non-capturing group. Groups the expression for logic (like applying a quantifier) but does not store the result. This improves performance.
  • (?<name>expression): Named capturing group. Assigns a custom name to the group for easier extraction in some languages.

6. Lookaround Assertions - Advanced

Lookarounds match a position based on what follows or precedes it, but they “do not consume” characters.

  • (?=...) Positive Lookahead: Asserts that what follows is .... Example: Windows(?=95|98|NT) matches “Windows” only if it is followed by 95, 98, or NT.
  • (?!...) Negative Lookahead: Asserts that what follows is NOT ....
  • (?<=...) Positive Lookbehind: Asserts that what precedes is ....
  • (?<!...) Negative Lookbehind: Asserts that what precedes is NOT ....

7. Escaping Special Characters

The following characters have special meanings in Regex: *, +, ?, [, ], (, ), {, }, ., ^, $, |, \.

To match them literally, you must escape them with a backslash \. For example, to match a literal period ., you should write \..

8. Common Flags

  • i (Ignore Case): Case-insensitive matching.
  • g (Global): Find all matches rather than stopping at the first one.
  • m (Multiline): Makes ^ and $ match the start and end of every line.
  • s (DotAll): Allows the dot . to match newlines.
  • u (Unicode): Enables Unicode support, essential for matching emojis or special characters correctly.

9. Real-world Examples

  • US Phone Number: ^\d{3}-\d{3}-\d{4}$
  • Email Address: ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
  • Date (YYYY-MM-DD): ^\d{4}-\d{2}-\d{2}$
  • IP Address (Basic Format): ^(?:\d{1,3}\.){3}\d{1,3}$ (Matches numeric format only)
  • Strong Password: ^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$ (Min 8 chars, 1 letter, 1 number)

10. Performance & Best Practices

  1. Avoid Catastrophic Backtracking: Be careful with nested quantifiers (e.g., (a+)+), which can cause the engine to hang.
  2. Use Non-capturing Groups: Use (?:...) unless you actually need to extract the data.
  3. Be Specific: Use [a-z] instead of . whenever possible. Specificity boosts speed and accuracy.
  4. Know Your Engine: While basics are standard, advanced features (like lookbehind) vary between JavaScript, Python, PHP, and other environments.

Conclusion

Regular Expressions are the “Swiss Army Knife” for every developer. Although the syntax might look intimidating at first, mastering it will empower you to handle complex text processing with ease.

To help you write and test Regex patterns effectively, we offer an online tester.

👉 Try it now: Online Regex Tester

Simply input your pattern and test string, and the tool will highlight matches in real-time with detailed syntax explanations.