Tools mentioned in this article
Open the browser-based tool while you read and try the workflow immediately.

Regular expressions are powerful, but building one from scratch every time is a chore. This article collects practical, copy-paste regex recipes, each paired with an explanation of what it matches. It also covers everyday text-processing tasks like character counting and escaping.
If you want to learn the fundamentals first (metacharacters and quantifiers), start with Regex Basics.
First, the Building Blocks
Before the recipes, here are the frequently used parts.
| Pattern | Meaning |
|---|---|
\d | a digit ([0-9]) |
\w | a word character (alphanumerics and underscore) |
\s | a whitespace character (space, tab, newline, …) |
. | any single character (except newline) |
+ | one or more of the previous |
* | zero or more of the previous |
? | zero or one of the previous |
{2,4} | 2 to 4 of the previous |
^ / $ | start / end of line |
Copy-Paste Recipes
Extract email addresses
A “fully correct” email regex is impractically complex. For extracting from logs or text, this practical pattern is enough.
[\w.+-]+@[\w-]+\.[\w.-]+
It matches addresses like [email protected].
Extract URLs
https?://[\w./?=&%#-]+
This handles both http:// and https:// (the s? means “s, zero or one time”).
Validate a date (YYYY-MM-DD)
^\d{4}-\d{2}-\d{2}$
Matches a string like 2026-06-19. The ^ and $ enforce “the whole line is exactly this format.”
Collapse repeated whitespace (replace)
A staple for cleaning up extra spaces from copy-paste.
Find: \s+
Replace: (a single space)
\s+ matches a run of consecutive whitespace, so it collapses to one space.
Reorder with capture groups
Swap First Last to Last First, referencing groups in the replacement.
Find: (\S+)\s+(\S+)
Replace: $2 $1
Taro Yamada → Yamada Taro (where $1 and $2 reference the captured parts).
Common Stumbling Blocks
- Greedy matching:
<.+>swallows everything “from the first<to the last>.” For the shortest match, add?:<.+?>. - Escaping metacharacters: to search for
.?([literally, escape with a backslash:\.. - Matching across newlines:
.normally doesn’t match newlines. To span lines, use[\s\S]or enable the tool’ss(dotall) flag.
To check whether your pattern works as intended, run it against real text in the regex tester and watch the highlights. You can preview replacements on the spot, too.
Let Tools Handle “Text Processing” Too
Some frequent tasks aren’t a job for regex at all.
- Character counting: checking the length of social posts or meta descriptions.
- HTML escaping: converting
<>&to entities so tags display literally. - Case conversion: switching between
snake_caseandcamelCase. - Deduplicating and sorting lines: tidying up lists.
These are collected in the text utilities tool. Using them alongside regex cuts down the small daily battles with text.
Frequently Asked Questions
I heard ? has two meanings in regex.
Correct. As a quantifier (a?) it means “zero or one of the previous.” Placed right after another quantifier (.+?) it makes the match “lazy” (shortest). The role changes depending on its position.
How do I strictly validate an email address?
A fully RFC-compliant regex becomes impractically complex. In practice, use a loose check (“contains one @ with characters on both sides”) and confirm validity by sending a verification email. For extraction, the in-text pattern above is sufficient.
Does regex behave differently across languages?
The core syntax is shared, but flag syntax (e.g. JavaScript’s /pattern/gi) and lookbehind support vary. A browser-based tester runs on the JavaScript engine, making it ideal for checking patterns before embedding them in front-end code.