Regular Expressions Explained: A Practical Guide

BY TOOLS.FUN  ·  MARCH 28, 2026  ·  6 min read

Regular expressions — regex or regexp for short — are a compact language for describing patterns in text. They are built into nearly every programming language and are indispensable for input validation, text parsing, log analysis, search-and-replace operations, and data extraction. Once you understand the core syntax, you can write powerful one-liners that would take dozens of lines of procedural string-manipulation code.

Related tools: use our Diff Tool to compare regex outputs, Character Counter to measure match lengths, and URL Encoder to test URL pattern matching.

What is a Regular Expression?

A regular expression is a sequence of characters that forms a search pattern. When you apply a regex to a string, the engine scans the string to find substrings that match the pattern. The pattern can describe something as simple as a literal word or as complex as an arbitrarily nested structure.

Most languages wrap regex in delimiters or pass it as a string. In JavaScript: /pattern/flags. In Python: re.compile(r'pattern'). The underlying engine behaviour is nearly identical across languages, though there are minor dialect differences.

Key point: Regex engines are "greedy" by default — they match as much as possible. Add a ? after a quantifier to make it lazy (match as little as possible).

Basic Regex Syntax

Literal characters match themselves: the pattern cat matches the string "cat" anywhere it appears. Metacharacters have special meaning and must be escaped with a backslash if you want to match them literally:

. ^ $ * + ? { } [ ] \ | ( )

The dot . matches any single character except a newline. So c.t matches "cat", "cot", "cut", and "c3t".

The pipe | is alternation — cat|dog matches either "cat" or "dog".

Quantifiers and Repetition

Quantifiers specify how many times a preceding element must appear:

colou?r      # matches "color" and "colour"
\d{4}        # matches exactly 4 digits
[a-z]{3,8}   # matches 3 to 8 lowercase letters
Key point: Add ? after any quantifier to make it lazy: .*? matches the shortest possible string rather than the longest.

Character Classes

A character class is a set of characters inside square brackets. The pattern matches any single character in the set:

[aeiou]       # any vowel
[a-zA-Z]      # any letter
[0-9]         # any digit (same as \d)
[^0-9]        # any character that is NOT a digit

Shorthand character classes save typing: \d (digit), \w (word character: letter, digit, underscore), \s (whitespace), and their negations \D, \W, \S.

Anchors and Boundaries

Anchors don't match characters — they match positions within the string:

^\d{5}$        # string must be exactly 5 digits (a US ZIP code)
\bword\b       # matches "word" but not "password" or "keyword"

Groups and Capturing

Parentheses create groups. A capturing group (...) extracts the matched text for later use. A non-capturing group (?:...) groups without extracting.

(\d{4})-(\d{2})-(\d{2})   # captures year, month, day separately
(?:https?|ftp)://              # non-capturing group for protocol

Named groups make code more readable: (?P<year>\d{4}) in Python, or (?<year>\d{4}) in most other engines.

Key point: Lookahead (?=...) and lookbehind (?<=...) match a position without consuming characters. Use them when you need to assert what comes before or after a match without including it in the result.

Common Regex Patterns

Here are production-tested patterns for common validation tasks:

# Email (simplified but practical)
^[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,}$

# URL
https?://[^\s/$.?#].[^\s]*

# US phone number (flexible)
^(\+1)?[\s.\-]?\(?\d{3}\)?[\s.\-]?\d{3}[\s.\-]?\d{4}$

# Hex colour
^#([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$

# ISO date
^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$

Regex Flags Explained

Flags modify how the engine interprets the pattern:

Key point: Catastrophic backtracking occurs when a poorly written regex causes exponential matching time on certain inputs. Avoid nested quantifiers like (a+)+ on untrusted input — they can bring a server to its knees.

Try it Live

The RegExp Tester at Tools.Fun lets you write a pattern and test it against sample strings in real time. Matches highlight as you type, flags are toggleable, and capture groups are listed individually — it's the fastest way to iterate on a regex without leaving your browser.

← Back