Regex for Beginners: Your First Ten Patterns
Most regex tutorials start with a syntax table and lose you by the third row. This one starts with work. Below are ten patterns that solve problems you will genuinely hit — searching, validating, cleaning, and reformatting text — and each one introduces exactly one new idea. Read them in order and you will have absorbed the core of the language without ever having memorized a chart. For the broader map of the syntax, including flags, groups, and performance, see The Complete Guide to Regular Expressions.
One habit before we start: never write a regex blind. Keep the Regex Tester & Matcher open in a tab, paste in a few strings that should match and a few that should not, and watch the highlighting as you type. Everything on this site runs entirely in your browser, so log lines and customer data you paste in never get uploaded anywhere.
1. Find a Whole Word: \bcat\b
The pattern cat matches the letters c-a-t anywhere — including inside "concatenate" and "scattered". \b is a word boundary, a zero-width position between a word character and a non-word character. Wrapping the word in boundaries makes the search exact.
Against the cat scattered concatenated cats, \bcat\b matches once: the standalone cat. Note that cats is not matched either, because s is a word character, so there is no boundary after cat.
2. Digits Only: ^\d+$
\d is shorthand for a single digit. + means "one or more of the thing before me." ^ anchors to the start of the string and $ to the end.
Together they say: from beginning to end, nothing but digits. 4021 matches. 40 21, 4021a, and the empty string do not. Drop the anchors and \d+ would happily match the 4021 inside order-4021-shipped, which is why every validation pattern needs anchors. This single mistake accounts for more broken validators than any other.
3. Collapse Runs of Whitespace: \s+
\s matches any whitespace character — space, tab, newline, carriage return, and a few exotic Unicode spaces. Combined with +, it matches a whole run of them at once.
Replace every match with a single space and hello \t\n world becomes hello world. This is the one-line fix for text scraped out of a PDF or a rendered web page. Try it in Regex Find & Replace with the global flag on.
4. Strip Trailing Whitespace: [ \t]+$
Square brackets define a character class: one character from the set inside. [ \t] means "a space or a tab," deliberately excluding newlines so we do not eat the line breaks we are trying to preserve.
With the m (multiline) flag, $ matches at the end of every line rather than only the end of the string, so this pattern cleans a whole file at once. Without m, it only touches the final line.
5. Validate a Hex Color: ^#(?:[0-9a-fA-F]{3}|[0-9a-fA-F]{6})$
Three new ideas at once. {3} is an exact count quantifier. | means "or." (?:...) is a non-capturing group — it bundles the two alternatives so the | applies only inside the group, without creating a numbered capture we would never read.
#fff and #a3c1F9 match. #ffff does not, because after the alternation the $ anchor demands the string is over. Order inside the alternation does not matter here: the engine tries the 3-character branch first, fails at $, backtracks, and tries the 6-character branch.
6. Reformat a Date: (\d{4})-(\d{2})-(\d{2})
Parentheses without ?: capture. Each group is numbered by the position of its opening parenthesis, starting at 1.
Against 2026-08-07, group 1 is 2026, group 2 is 08, group 3 is 07. In a replacement string, reference them as $1, $2, $3 (JavaScript, .NET, PHP) or \1, \2, \3 (Python, sed). Replacing with $3/$2/$1 yields 07/08/2026. This is the workhorse pattern of bulk text editing.
7. Match a Phone Number: ^\(?\d{3}\)?[-. ]?\d{3}[-. ]?\d{4}$
? means "zero or one" — the preceding element is optional. \( and \) are escaped literal parentheses, since bare parentheses would create a group.
This accepts (555) 123-4567, 555-123-4567, 555.123.4567, and 5551234567. Note the space inside [-. ]: it is a literal space in the set. Also note that the hyphen sits first in the class, where it is treated literally rather than as a range operator — [.-] would work too, but [a-.] would be an error.
Real phone validation across countries is much harder than this. Treat the pattern as a typo catcher for a single locale, not a truth test.
8. Extract a URL: https?://[^\s)]+
s? makes just the s optional, so both http:// and https:// match — a quantifier binds to the single element immediately before it, not the whole word.
[^\s)] is a negated character class: the leading ^ inside the brackets flips it to "any character that is not whitespace and not a closing parenthesis." Excluding ) stops the match from swallowing the bracket when a URL appears in prose like (see https://example.com/docs).
Against Docs at https://example.com/a/b?x=1 today, it matches https://example.com/a/b?x=1 and stops at the space.
9. Strip HTML Tags: <[^>]+>
Same idea, applied usefully: <, then one or more characters that are not >, then >. Because the negated class physically cannot cross a >, the match can never run past the end of a tag.
On <p>Hello <b>there</b></p> it matches <p>, <b>, </b>, and </p> individually. Compare that with <.+>, which matches the entire string in one go because . matches > too — the classic greedy-matching trap covered in depth elsewhere in this cluster. Use this for quick cleanup of simple markup only; anything structural belongs in a real parser.
10. Find Doubled Words: \b(\w+)\s+\1\b
\w is a word character (letter, digit, or underscore). The new piece is \1, a backreference: it matches the exact text that group 1 captured, not the pattern again.
So this reads: a word boundary, a word (captured), some whitespace, then that same word again. Against this is is a a test, it finds is is and a a. Add the i flag to catch The the at the start of a sentence. It is the fastest proofreading pass you can run on your own writing.
Where to Go Next
Those ten patterns cover the four building blocks that make up almost everything else: character classes say what, quantifiers say how many, anchors say where, and groups say which part matters. Nearly every intimidating pattern you meet is just those four, stacked.
The natural next step is going deeper on the first two. Regex Character Classes and Quantifiers Explained covers the edge cases — Unicode-aware classes, why [a-Z] is an error, and the {n,m} forms — that turn guesswork into confidence. After that, How to Validate an Email Address with Regex (and Why Not To) is a good reality check on the limits of pattern matching.
When you inherit a pattern nobody remembers writing, paste it into the Regex Explainer & Pattern Breakdown — it decomposes a pattern token by token, which is far quicker than parsing nested groups in your head.
Frequently asked questions
What is the easiest way to learn regex?
Learn by reading and modifying working patterns rather than memorizing syntax tables. Pick five patterns you actually need — a whole-word search, a digits-only check, a whitespace cleanup, a date reformat, a tag stripper — paste each into a live tester with sample text, then change one character at a time and watch what breaks. Ten patterns cover most day-to-day use.
What does \b mean in regex?
\b is a word boundary: a zero-width position between a word character (letter, digit, or underscore) and anything else, including the start or end of the string. It matches a position, not a character, so \bcat\b matches cat in "the cat sat" but not inside "concatenate".
How do I match an exact whole string in regex?
Anchor both ends with ^ and $. The pattern \d+ finds digits anywhere inside abc123def, while ^\d+$ matches only strings that are digits from beginning to end. Validation patterns should almost always be anchored, or they will accept any string that merely contains a valid fragment.
What is the difference between * and + in regex?
* means zero or more repetitions of the preceding element, so it can match nothing at all. + means one or more, requiring at least one occurrence. \d* matches an empty string, while \d+ requires at least one digit.
How do I replace text using capture groups?
Wrap the parts you want to keep in parentheses, then reference them in the replacement string. In JavaScript, .NET, and PHP use $1, $2, $3; in Python and sed use \1, \2, \3. Matching (\d{4})-(\d{2})-(\d{2}) and replacing with $3/$2/$1 turns 2026-08-07 into 07/08/2026.
Do I need to escape special characters in regex?
Yes, for the twelve metacharacters . ^ $ * + ? { } [ ] \ | ( ). Put a backslash in front to match them literally, so \. matches a period and \$ matches a dollar sign. Inside a character class most of them lose their special meaning, so [.] is a valid alternative to \.
Should I use regex to parse HTML?
Not for real parsing. A pattern like <[^>]+> is fine for a quick cleanup of simple, trusted markup, but nested tags, attributes containing angle brackets, comments, and CDATA sections will defeat it. Use an HTML parser when correctness matters.
Try the related tools
Regex Find & Replace
Find and replace text using regular expressions and capture groups.
Regex Tester & Matcher
Test regular expressions with real-time match highlighting, flags, and capture groups.
Regex Explainer & Pattern Breakdown
Deconstruct and explain regular expression tokens, quantifiers, and groups in plain English.
Related articles
The Complete Guide to Regular Expressions
A complete regex guide covering regex syntax, character classes, quantifiers, groups, flags, and performance — with a regex cheat sheet and free live tools.
Regex Character Classes and Quantifiers Explained
How regex character classes and quantifiers really work: ranges, negation, \d \w \s across engines, Unicode classes, and every repetition form with its gotchas.
How to Validate an Email Address with Regex (and Why Not To)
An email validation regex that actually works, what it accepts and rejects, why no regex for email can be RFC-correct, and what to do instead of trying.