Regex Recipes Image

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 user.name+tag@example.co.jp.

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’s s (dotall) flag.

To check whether your pattern works as intended, run it against real text in the regex tester and watch the highlights. Tick “Also preview the text after replacement” and enter a replacement string, and you can check the result of a replacement using $1, $<name> and $& on the spot.

Results actually checked in the tester

These are results from the regex tester. The tester runs JavaScript regular expressions and returns the position of each match (index, a character position counted from 0), the matched text, the capture groups and the named groups.

Pattern (flags) Input Result
(\d{4})-(\d{2})-(\d{2}) (g) 2026-06-19 and 2026-07-01 2 matches (positions 0 and 15). Groups 2026 / 06 / 19
Same pattern (no flags) Same input Only the first match (position 0)
(?<y>\d{4})-(?<m>\d{2}) (g) 2026-06 Named groups y = 2026, m = 06
<.+> (g) <a><b> 1 match: all of <a><b>
<.+?> (g) <a><b> 2 matches: <a> and <b>
a.b (no flags) a and b on separate lines No match
a.b (s) Same input Matches (. also matches the newline)
^\w+$ (gm) one / two on separate lines 2 matches (one per line)
a* (g) baab Positions 0, 1, 3 and 4, including matches of the empty string
東京 (g) 東京駅と東京湾 Positions 0 and 4
\p{L}+ (gu) Hello 東京 Hello and 東京 (\p{...} needs the u flag)

Points to keep in mind when reading results.

  • Without g, only the first match is returned. Add g when you want them all.
  • A pattern that allows zero repetitions, such as *, also matches the empty string. If unwanted empty matches appear, consider whether + would do.
  • Positions count an emoji as two characters (in a😀b, 😀 is at position 1 and the next character is at position 3).
  • With an invalid pattern (such as an unclosed () or a nonexistent flag, an error message is shown instead of matches. An empty pattern gives no matches rather than an error.

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_case and camelCase.

Character counting and HTML escaping are in the text utilities tool, and case conversion is in the Case Converter. 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.