Once you know the basics of regular expressions, the fastest way to improve is to see patterns used on real problems. This collection covers twenty patterns that come up again and again when validating forms, cleaning text and extracting information. Each one is explained so that you can adapt it, not just copy it. Test every pattern with your own sample data before you rely on it, because regex flavors differ slightly between languages.
Before You Start
A few reminders will prevent most surprises:
- Anchors ^ and $ force a pattern to match the whole string. Without them, the pattern matches anywhere inside it.
- Backslashes must be doubled in many programming-language strings, for example \\d instead of \d.
- Validation patterns usually check the shape of the data, not whether it truly exists. A regex cannot tell you if an email inbox is real.
- Always combine client-side checks with server-side validation.
Validation Patterns
1. Digits only
^\d+$
Matches one or more digits and nothing else. Useful for numeric codes.
2. Whole number, optionally negative
^-?\d+$
The question mark makes the minus sign optional.
3. Decimal number
^-?\d+(\.\d+)?$
Accepts values like 12, -4 and 3.14. The group in parentheses is optional.
4. Letters only
^[A-Za-z]+$
Limits input to English letters. For names in many languages, use a more permissive approach or Unicode-aware pattern support.
5. Username
^[a-zA-Z0-9_]{3,16}$
Allows 3 to 16 letters, digits or underscores.
6. URL slug
^[a-z0-9]+(?:-[a-z0-9]+)*$
Lowercase words separated by single hyphens, with no leading or trailing hyphen.
7. Basic email shape
^[^\s@]+@[^\s@]+\.[^\s@]+$
Checks for text, an at sign, text, a dot and text. It is intentionally simple, because perfect email validation with regex is notoriously difficult.
8. Hex color code
^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$
Matches six or three hexadecimal digits with an optional hash sign.
9. Date in year-month-day format
^\d{4}-\d{2}-\d{2}$
Checks the shape only. February 31 would pass, so verify with a date function afterwards.
10. 24-hour time
^([01]\d|2[0-3]):[0-5]\d$
Hours from 00 to 23 and minutes from 00 to 59.
11. IPv4 address, strict
^((25[0-5]|2[0-4]\d|1?\d?\d)\.){3}(25[0-5]|2[0-4]\d|1?\d?\d)$
Each of the four parts must be between 0 and 255.
12. Loose phone number
^\+?[\d\s\-().]{7,20}$
Allows digits, spaces, hyphens, brackets and a leading plus sign. Phone formats vary widely by country, so keep validation relaxed and normalize the number afterwards.
13. Password with minimum requirements
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$
The lookaheads require a lowercase letter, an uppercase letter and a digit, with at least eight characters. Length matters more than complexity rules, so consider requiring a longer minimum instead.
Cleaning and Searching Patterns
14. Extra whitespace
\s{2,}
Finds two or more whitespace characters in a row. Replace with a single space.
15. Leading and trailing spaces
^\s+|\s+$
Matches spaces at the start or end. Replace with nothing to trim.
16. Blank lines
^\s*$\n
Use the multiline flag. Replace with nothing to remove empty lines.
17. Repeated words
\b(\w+)\s+\1\b
Use the case-insensitive flag. The backreference \1 matches the same word again, catching typos like "the the".
18. HTML tags (simple removal only)
<[^>]+>
Strips basic tags from text. It is not a safe way to sanitize HTML for security, and a real HTML parser is better for anything complex.
Extraction Patterns
19. Hashtags and mentions
[#@]\w+
Finds words beginning with a hash or at sign, useful for analyzing social media text.
20. Rearranging a date
(\d{4})-(\d{2})-(\d{2})
Replacement: $3/$2/$1
The three groups capture year, month and day. The replacement outputs day, month and year, turning 2026-12-05 into 05/12/2026. Some languages use \3/\2/\1 instead of the dollar syntax.
Tips for Writing Your Own Patterns
- Start with the simplest version that matches your example, then add restrictions.
- Write down both strings that should match and strings that must not match, and test all of them.
- Prefer specific character classes over a greedy dot-star where possible.
- Use non-capturing groups (?:...) when you do not need the captured text.
- Add comments or document complicated patterns so that you can understand them later.
- Avoid nested quantifiers like (a+)+ that can cause extremely slow matching on certain inputs.
Practice in a Live Tester
The quickest way to learn is to paste a pattern and some sample text into a tester, and watch the matches highlight in real time. Our free Regex Tester shows matches and capture groups instantly and runs entirely in your browser.
Final Thoughts
You do not need to memorize every symbol. Keep a small library of trusted patterns like these, understand each part and adjust them to your data. Test with realistic examples, and remember that regex is a tool for shaping and finding text, not a replacement for proper validation logic.