Regular Expressions Explained: A Beginner Guide with Live Examples
Regular expressions — often called regex or regexp — are patterns that describe sets of strings. They are used to search, validate, extract, and replace text in almost every programming language and many text editors. They look intimidating at first, but the core syntax covers about 90% of real-world use cases.
What is a regular expression?
A regex pattern is a sequence of characters that defines a search pattern. The pattern \d+ means "one or more digit characters." Run it against the string "Order 1042 placed on June 12" and it matches "1042" and "12".
The most useful regex syntax
Character classes
\d— any digit (0–9)\w— any word character (a–z, A–Z, 0–9, underscore)\s— any whitespace (space, tab, newline).— any character except newline[abc]— any one of a, b, or c[^abc]— anything except a, b, or c[a-z]— any lowercase letter a through z
Quantifiers
*— zero or more+— one or more?— zero or one (optional){3}— exactly 3{2,5}— between 2 and 5
Anchors
^— start of string (or line with m flag)$— end of string (or line with m flag)\b— word boundary
Groups and alternation
(abc)— capture group: captures the matched text as a group(?:abc)— non-capturing group: groups without capturinga|b— matches a or b
Common regex patterns
Email address (simplified):
[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}URL:
https?:\/\/[\w\-.]+\.[a-z]{2,}(\/[\S]*)?Date in YYYY-MM-DD format:
\d{4}-\d{2}-\d{2}Phone number (flexible):
[+]?[\d\s\-().]{7,15}Hex colour code:
#[0-9a-fA-F]{3,6}\bFlags
g(global) — find all matches, not just the firsti(case-insensitive) — match regardless of letter casem(multiline) —^and$match start/end of each line
Testing your patterns
The Regex Tester tool below lets you paste a pattern, toggle flags, enter test text, and see all matches highlighted in real time. Capture groups from the first match are shown below the test string.
Frequently asked questions
What does . mean in regex?
A bare dot matches any single character except a newline. To match a literal period use \. (escaped dot). This is one of the most common beginner mistakes — writing . when you mean \..
What is the difference between + and *?
+ means one or more — the pattern must match at least once. * means zero or more — the pattern may not match at all. \d+ matches "123" and "5" but not an empty string. \d* matches "123", "5", and also an empty string.
Why does my regex match too much?
Quantifiers are greedy by default — they match as much as possible. Add ? after a quantifier to make it lazy: .+? matches the shortest possible string. This is the fix for patterns that match from the first occurrence all the way to the last instead of stopping at the first natural end.
Are regex patterns the same in all programming languages?
Most languages (JavaScript, Python, PHP, Ruby, Java) use a similar PCRE-based syntax. Minor differences exist — Python uses \A and \Z for string boundaries, JavaScript does not support lookbehind in older engines. Test patterns in your specific environment before relying on them in production.