🏠 Home
CPS Test Aim Trainer Typing Speed Scroll Speed View All Games →
AI Image Generator Background Remover Social Media Cropper Youtube Thumbnails View All Images →
Word Counter Case Converter Invisible Text Text to Speech View All Text Tools →
JSON Formatter Diff Checker Base64 Converter Meta Tag Generator View All Dev Tools →
Unit Converter Age Calculator BMI Calculator Time Zone Converter View All Calculators →
Home / Blog / Regex 101

Regex 101: 20 Patterns You Will Actually Use



Brass letterpress characters, a magnifying glass and chalked bracket symbols on a workbench

Regular expressions have a reputation problem. The syntax looks like a cat walked across the keyboard, the documentation reads like a legal contract, and every second tutorial opens with a pattern nobody would ever type. The useful truth is smaller: a handful of characters covers almost everything you will do in practice. Validate an email-ish string. Pull dates out of a log. Collapse stray whitespace. That is the real job, and twenty patterns handle most of it — provided you test them on real text before they reach production.

The Vocabulary Worth Memorizing

Every pattern is built from the same short list. \d is a digit, \w a word character, \s whitespace, and . almost anything. Brackets create a set ([abc]), parentheses create a group, and | means either side. Quantifiers decide repetition: * zero or more, + one or more, ? optional, {2,5} a counted range. Anchors ^ and $ pin a match to the start and end of a line or string. Learn greedy versus lazy (.* versus .*?) and you have enough vocabulary to read most patterns you will meet.

Validation Patterns

For validation, anchor the pattern at both ends. Without anchors, a pattern happily approves a substring and lets the rest of the input through.

  1. ^[^\s@]+@[^\s@]+\.[^\s@]+$ — Email, loosely. It checks for one @ and a dot in the domain, nothing more. Perfect email validation is a famous rabbit hole; send a confirmation message instead.
  2. ^https?:\/\/[^\s]+$ — URL with a scheme. Requires http or https, then anything that is not whitespace. It will accept nonsense paths, so pair it with an actual request.
  3. ^\d{5}(?:-\d{4})?$ — US ZIP code. Five digits, optionally followed by a hyphen and four more. The (?:) group is non-capturing because you do not need the extension separately.
  4. ^\+?[\d\s().-]{7,15}$ — Phone, permissive. Allows a leading plus, digits, spaces, parentheses, dots and hyphens. Count the digits in code afterward; patterns that hard-code a country format reject too many real numbers.
  5. ^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$ — Password with mixed classes. Each lookahead checks a requirement without consuming characters. Current guidance tends to favor length and a blocklist over strict composition rules, so treat this as a floor, not a ceiling.
Pro Tip: When a validation regex fails, log the exact input that failed — including invisible characters. A trailing carriage return or a non-breaking space will make a correct pattern look broken, and you will lose an hour debugging the wrong thing.

Extraction Patterns

Extraction usually wants the opposite of validation: no anchors, and a capture group around the part you actually need.

  1. (?<![\w#])#\w+ — Hashtags. The lookbehind stops a match in the middle of a word such as page#section. It still matches the hashtag in ordinary prose and social captions.
  2. (?<![\w@])@[A-Za-z0-9_]{1,30} — Mentions. Similar guard, then an @ followed by up to thirty handle characters. Adjust the upper bound to match the platform you are parsing.
  3. \b\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])\b — ISO dates. Restricts month and day ranges, but it will still accept February 31. Calendar correctness belongs in your date library, not in the pattern.
  4. (\w+)=("([^"]*)"|\S+) — Key-value pairs. Group 1 is the key, group 3 the quoted value, group 2 the whole value. Nested groups look fussy until you write the parsing code and appreciate them.
  5. <\/?[A-Za-z][^>]*> — HTML tags, superficially. Good enough to strip tags from plain prose. Do not use it to parse a document; nested markup and quoted angle brackets defeat regex, and a real HTML parser is the honest tool.

Cleanup Patterns

Cleanup is where regex saves the most tedium, because these edits are repetitive and easy to describe. Test each replacement on a sample before running it across a whole file.

  1. [ \t]{2,} → a single space — Collapse runs of spaces. Useful after copy-paste from PDFs and spreadsheets. Restrict it to prose, since it will also flatten intentional code indentation.
  2. [ \t]+$ with the multiline flag — Trailing whitespace. Matches spaces and tabs at the end of each line. Replace with nothing and your diffs get quieter.
  3. (?:\r?\n){3,} → two newlines — Duplicate blank lines. Handles both Unix and Windows line endings, which is the detail most cleanup snippets forget.
  4. [\u2018\u2019\u201C\u201D] — Curly quotes. Matches smart quotes copied from word processors. Replace them one by one, because left and right curls map to different straight characters.
  5. \b0+(?=\d) — Leading zeros. The lookahead keeps the final digit, so 00042 becomes 42 while a lone 0 survives. Run it on numeric fields only; ZIP codes and product IDs need their zeros.

Developer and Log Patterns

These show up in build scripts, log filters and quick inspections. Treat them as shape checks: they tell you what something looks like, not whether it is valid or trustworthy.

  1. #(?:[0-9a-fA-F]{6}|[0-9a-fA-F]{3})\b — Hex colors. Six digits first, then the shorthand form, with a word boundary so a longer token does not match halfway. The same idea works for other fixed-width identifiers.
  2. \b(?:\d{1,3}\.){3}\d{1,3}\b — IPv4-shaped strings. Any four dot-separated numbers, including impossible ones such as 999.999.999.999. Validate octet ranges in code if it matters.
  3. ^eyJ[A-Za-z0-9_-]*\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]*$ — JWT shape. Three dot-separated URL-safe segments, usually starting with the base64 of a JSON header. It proves nothing about the signature; only verification does that.
  4. \b(?:DEBUG|INFO|WARN|ERROR|FATAL)\b — Log levels. Word boundaries prevent matching ERROR inside a longer identifier. Add the case-insensitive flag if your logger is inconsistent.
  5. ^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?(?:\+[0-9A-Za-z.-]+)?$ — Semantic versions. Covers the major, minor and patch core plus optional pre-release and build metadata. It does not enforce ordering, which is a comparison problem rather than a matching one.

Regex FAQs

Why does my pattern match only part of a word?

You are probably missing boundaries or anchors. \bcat\b matches the standalone word and skips concatenate; ^ and $ pin the match to a whole line. Add one, then retest against both a match and a near-miss.

Should I use greedy or lazy quantifiers?

Greedy is the default and grabs as much as possible; lazy grabs as little. For quoted strings and paired tags, lazy is usually what you meant, since ".*" can swallow everything up to the last quote on the line.

Can one regex validate email addresses or HTML?

Not reliably. Email grammar is broader and stranger than most patterns assume, and HTML is nested rather than regular. Use a loose pattern for a first pass, then confirm with a real parser or a verification email. That combination is more accurate and far easier to maintain.

Copy any pattern above into the free Regex Tester and paste your own data underneath it. Watching the highlights land on the right characters, and miss the wrong ones, is worth more than another page of syntax notes. If you want a deeper walkthrough of flags and backtracking, the regex testing guide covers that next; for cleaning up the text you just matched, the word counter is a quick sanity check.

Twenty patterns, one place to try them.

Test Regex Free

Share This Tool

⭐
Enjoying NoLoginTool?

Save it for later access 🚀