HomePluginsToolsGuidesPortfolioWorkflowAboutWork with usContact
GetSet

We build and fix production web applications, and publish the UI packages and free browser tools on this site.

info@get-set.it

Explore

  • Plugins
  • Tools
  • Guides
  • Portfolio

Company

  • About
  • Workflow
  • Work with us
  • Contact

Legal

  • Terms
  • Privacy
  • Accessibility
  • Cookies

© 2026 GetSet

We're on npm

Your Bundle

Your bundle is empty

Add plugins from the catalog to build one combined CDN link — like picking font families.

Browse plugins
Guides / Code

Regular Expressions You Actually Use: A Practical Field Guide

The 20% of regex that solves 90% of problems — anchors, classes, quantifiers, groups and lookarounds — plus the catastrophic-backtracking trap and when not to use regex at all.

By the GetSet engineering team · Published 2026-08-22 · 8 min read · Related tool: CodeFormat Dev Tools

The building blocks worth memorizing

  • Anchors ^ $ — start and end of input (or of each line with the m flag). Forgetting them is the #1 reason "validation" accepts garbage: \d{4} matches "abc2026xyz".
  • Classes \d \w \s and their negations \D \W \S; custom sets [A-Fa-f0-9]; . matches anything but newline.
  • Quantifiers * (0+), + (1+), ? (0–1), {n,m}; append ? to make them lazy (.*?) so they stop at the first possible match.
  • Groups ( ) capture, (?: ) group without capturing, (?<name> ) names the capture so code reads match.groups.year instead of match[3].
  • Alternation cat|dog — lower precedence than everything, so ^cat|dog$ means "starts with cat OR ends with dog"; wrap it: ^(?:cat|dog)$.

Lookarounds: matching context without consuming it

Lookahead (?=…) and lookbehind (?<=…) (plus their negatives (?!…) (?<!…)) assert that something follows or precedes without including it in the match. They make otherwise-awkward jobs trivial: \d+(?= USD) grabs the number but not the currency; (?<!\$)\b\d+ finds numbers not preceded by a dollar sign; ^(?=.*[A-Z])(?=.*\d).{8,}$ is the classic password-policy check (at least one uppercase, one digit, eight characters).

Catastrophic backtracking — the regex that hangs your server

Nested quantifiers over overlapping patterns — (a+)+$, (\w+\s?)*$, (.*a){20} — can take exponential time on inputs that *almost* match. A 30-character string can keep the engine busy for minutes, and attackers know it (ReDoS). Rules: never nest unbounded quantifiers; prefer specific classes over .*; anchor early; and for user-supplied text, set a length limit before matching. Modern engines with linear-time guarantees (RE2, Rust’s regex) exist precisely because this is so easy to get wrong.

Test patterns live against sample text with match highlighting and group capture in the regex tester inside CodeFormat’s dev tools — along with a cron expression parser, JWT decoder and UUID generator. It runs offline; your test data stays on your machine.

When not to use regex

  • HTML, XML, JSON — nested structures are not regular languages; use a parser. Regex will work on the sample and fail on production.
  • Email addresses — the fully correct pattern is thousands of characters; check for @ and a dot, then send a verification email.
  • Dates — \d{2}/\d{2}/\d{4} happily accepts 99/99/9999. Parse with a date library.
  • Anything you cannot read back next month — if it needs a comment longer than the pattern, split it into named groups with the x (verbose) flag or into code.

A regex is a scalpel: unmatched for slicing text by pattern, wrong for structural surgery. Knowing which job is which is most of the skill.

Keep reading

Images

WebP vs AVIF vs PNG vs JPG: Choosing the Right Image Format in 2026

7 min read
Data

CSV, JSON and XLSX: The Conversion Pitfalls That Corrupt Your Data

8 min read
Code

Minify vs Beautify: What Code Formatting Actually Does to Your Files

6 min read

We write these guides from our client work. If you’d rather have it built than explained, we’re available.

Work with us Contact us