Regular Expressions Explained: A Practical Guide
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.
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.
? 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:
*— zero or more times+— one or more times?— zero or one time (makes the element optional){n}— exactly n times{n,}— n or more times{n,m}— between n and m times (inclusive)
colou?r # matches "color" and "colour"
\d{4} # matches exactly 4 digits
[a-z]{3,8} # matches 3 to 8 lowercase letters
? 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:
^— start of string (or start of line in multiline mode)$— end of string (or end of line in multiline mode)\b— word boundary (between a word character and a non-word character)\B— non-word boundary
^\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.
(?=...) 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:
i— case-insensitive matchingg— global: find all matches, not just the first (JavaScript)m— multiline:^and$match start/end of each lines— dotall:.matches newlines toox— verbose: allows whitespace and comments inside the pattern (Pythonre.VERBOSE)
(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