Regular Expressions for Beginners: The 10 Patterns That Solve 80% of Problems
By Marcus Vane · September 15, 2026 · Developer Tools
You do not need to master the whole regex language to be productive. Ten patterns cover the bulk of validation, extraction and cleaning tasks you will actually meet.
Regular expressions have a reputation for being opaque, and the full language is genuinely large. But the day to day reality is that a small set of patterns handles the overwhelming majority of tasks you will encounter: validating an email shape, pulling numbers out of text, splitting on flexible delimiters, trimming whitespace, and redacting sensitive values. Learn these ten and you will reach for a regex reference far less often than you expect.
Each pattern below is shown with a concrete input and what it matches, so you can test it directly in a regex tester and see the result before adapting it to your own data.
In this article:
- The ten patterns
- Worked examples in combination
- Common mistakes beginners make
- FAQ: regex edge cases
- Wrapping up
The ten patterns
1. Literal text
The simplest pattern matches itself. The pattern "error" finds the word error anywhere in a string. Most characters are literals; the punctuation characters that are not, such as the dot and asterisk, are called metacharacters and must be escaped with a backslash when you mean them literally.
2. Any character
A dot matches any single character except a newline. The pattern "c.t" matches cat, cot and cut. It is the wildcard of regex and the source of many beginner bugs, because people use it when they mean a literal dot, which must be written as "\.".
3. Character classes
Square brackets define a set of allowed characters. "[aeiou]" matches any one vowel, and "[0-9]" matches any digit. A leading caret negates the class, so "[^0-9]" matches anything that is not a digit. Classes are how you express "one of these" cleanly.
4. Shorthand classes
"\d" matches a digit, "\w" matches a word character which is letters, digits and underscore, and "\s" matches whitespace. Their uppercase forms "\D", "\W" and "\S" match the opposite. These cover most of what beginners use character classes for, with far less noise.
5. Quantifiers
A quantifier says how many times the preceding element repeats. The asterisk means zero or more, the plus means one or more, the question mark means zero or one, and curly braces give an exact range. "\d{3}" matches exactly three digits, and "\d{2,4}" matches between two and four.
6. Anchors
The caret anchors to the start of the string and the dollar sign to the end. "^\d" matches a digit only at the beginning, and "\d$" matches a digit only at the end. Anchors are what turn a partial match into a full string validation.
7. Groups and alternation
Parentheses group part of a pattern, and the pipe means or. "(cat|dog)" matches either word. Groups also capture their content for later use, which is how you extract a specific portion of a match.
8. Optional and repeating segments
Combining groups with quantifiers expresses optional structure. "https?" matches http or https because the s is optional. "colou?r" matches both the British and American spellings. This is the pattern that handles dialect and variant spelling gracefully.
9. Boundaries
"\b" matches a word boundary, the position between a word character and a non word character. "\bcat\b" matches cat as a whole word but not the cat inside category. Boundaries are essential when you are matching words that could appear as substrings of other words.
10. Lazy quantifiers
By default quantifiers are greedy and match as much as possible. Adding a question mark makes them lazy, matching as little as possible. The pattern "<.*?>" matches a single HTML tag, while "<.*>" greedily matches from the first opening bracket to the last closing one in the whole document.
Worked examples in combination
Example one: validate an email shape
A pragmatic email pattern is "^[\w.+-]+@[\w-]+\.[\w.-]+$". Against "jane.doe+work@example.co.uk" it matches the whole string. It does not verify the domain exists, but it catches typos like a missing at sign or a space in the middle. For real validation, send a confirmation message; regex only checks shape.
Example two: extract all prices from text
Given "The shirt costs $34.99 and the hat was 12.50.", the pattern "\$?\d+\.\d{2}" finds "$34.99" and "12.50". The optional dollar sign, one or more digits, a literal dot, and exactly two decimal digits capture currency formatted numbers while ignoring plain integers.
Example three: trim and normalise whitespace
To collapse multiple spaces into one, replace "\s+" with a single space. To remove leading and trailing whitespace, replace "^\s+|\s+$" with an empty string. These two operations clean up almost any pasted text before further processing.
Common mistakes beginners make
- Using a dot when you mean a literal dot, asterisk or question mark. Escape metacharacters with a backslash.
- Forgetting anchors when validating, so "123" matches inside "abc123def" and passes a check it should fail.
- Writing greedy quantifiers where lazy ones are needed, producing matches that span far more than intended.
- Overusing regex for parsing nested structures like HTML or JSON, which regex cannot handle reliably. Use a proper parser instead.
- Not testing against edge cases: empty strings, strings with only delimiters, and inputs with unexpected characters.
- Assuming "\w" matches only letters. It also matches digits and underscore, which surprises people validating names.
FAQ: regex edge cases
Why does my pattern match nothing on a multiline string?
The dot does not match newlines by default, and the anchors match the start and end of the whole string rather than each line. Most engines offer a multiline flag that changes both behaviours. Enable it when processing text that contains line breaks.
Are regex patterns the same across languages?
The core syntax is broadly shared, but advanced features differ between engines. A pattern that works in JavaScript may behave differently in Python or a database. Test in the engine you will actually run.
When should I not use regex?
For parsing nested or recursive structures such as HTML, XML, JSON and programming language source, a dedicated parser is more reliable and more readable. Regex is ideal for flat, line oriented text and simple token extraction.
Wrapping up
Ten patterns carry most of the practical load: literals, the dot, classes, shorthands, quantifiers, anchors, groups, optional segments, boundaries and lazy matching. Combine them in a tester against real inputs, and you will solve the majority of text problems without ever opening the full specification.
Test and refine your patterns live with instant match highlighting.
SwiftMultiTool — free online tools for everyone. Home · Blog · About · FAQ · Contact