Dev Utilities·7 min read·By the StackUtils Team

Regular Expressions: A Practical Guide with Examples

Regex can feel like magic — or a nightmare. This guide covers character classes, quantifiers, groups, and real-world patterns for email, URL, and date validation.

What Are Regular Expressions?

A regular expression (regex or regexp) is a sequence of characters that defines a search pattern. They are used to find, match, extract, or replace text based on that pattern. Regular expressions are supported natively in virtually every programming language: JavaScript, Python, Ruby, Java, Go, PHP, and more.

At their most basic, a regex like cat matches the literal string “cat”. The real power comes from metacharacters — special symbols that represent classes of characters, repetition, positions, and logical alternatives.

Character Classes

Character classes let you match a set of characters with a single token:

TokenMatchesExample
.Any character except newline/c.t/ → cat, cut, cot
\dAny digit (0–9)/\d+/ → 42, 2026
\DAny non-digit/\D+/ → abc, hello
\wWord character [a-zA-Z0-9_]/\w+/ → hello_world
\WNon-word character/\W+/ → !@#
\sWhitespace (space, tab, newline)/\s+/ → splits on spaces
\SNon-whitespace/\S+/ → hello
[abc]Any of a, b, or c/[aeiou]/ → vowels
[^abc]Anything NOT a, b, or c/[^0-9]/ → non-digits
[a-z]Any lowercase letter/[a-zA-Z]/ → letters

Quantifiers: How Many Times?

Quantifiers specify how many times the preceding token must occur to produce a match:

QuantifierMeaningExample
*0 or more/ab*c/ → ac, abc, abbc
+1 or more/ab+c/ → abc, abbc (not ac)
?0 or 1 (optional)/colou?r/ → color, colour
{n}Exactly n times/\d{4}/ → 2026
{n,}n or more times/\d{3,}/ → 100, 2026
{n,m}Between n and m times/\d{2,4}/ → 12, 123, 1234
*?Lazy: 0 or more (shortest match)/<.+?>/ → matches each tag separately

By default, quantifiers are greedy — they match as many characters as possible. Adding ? after a quantifier (e.g. +?) makes it lazy, matching the fewest characters possible.

Anchors and Groups

Anchors match positions rather than characters:

  • ^ — Start of string (or line in multiline mode)
  • $ — End of string (or line in multiline mode)
  • \b — Word boundary (between a word character and a non-word character)

Groups let you capture or organize parts of a pattern:

  • (abc) — Capturing group: stores the match for later use
  • (?:abc) — Non-capturing group: groups without storing
  • (?<name>abc) — Named capturing group: accessible as groups.name
  • a|b — Alternation: matches either a or b

Practical Patterns

Here are battle-tested regex patterns for common validation tasks. Note: for production use, library-level validators (e.g. Zod, Yup) are often preferable for complex formats like email.

Email (simplified)

^[^\s@]+@[^\s@]+\.[^\s@]{2,}$

Covers 99% of real email addresses. RFC 5322 is far more complex but rarely needed.

URL (http/https)

^https?:\/\/[\w-]+(\.[\w-]+)+([\w\-._~:/?#[\]@!$&'()*+,;=]*)?$

Matches http:// and https:// URLs. Does not cover ftp:// or protocol-relative URLs.

ISO 8601 Date (YYYY-MM-DD)

^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$

Validates format only, not calendar correctness (Feb 31 would pass).

IPv4 Address

^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$

Each octet: 0–255. Does not validate reserved ranges.

HEX Color Code

^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$

Matches #RGB and #RRGGBB formats. For alpha support, add |[A-Fa-f0-9]{8}|[A-Fa-f0-9]{4}.

Slug (URL-friendly)

^[a-z0-9]+(?:-[a-z0-9]+)*$

Lowercase letters and digits only, separated by single hyphens.

Regex Flags

Flags modify how the regex engine processes the pattern:

FlagNameEffect
gGlobalFind all matches, not just the first
iCase-insensitive/hello/i matches Hello, HELLO, hElLo
mMultiline^ and $ match start/end of each line
sDotall. matches newline characters too
uUnicodeEnables full Unicode matching (surrogate pairs, etc.)
dIndicesAdds start/end index to each match (ES2022)

Lookaheads and Lookbehinds

Zero-width assertions let you check what comes before or after a match without including it in the match itself:

  • (?=...) Positive lookahead — matches if followed by the pattern. /\d(?= dollars)/ matches the digit in “5 dollars”.
  • (?!...) Negative lookahead — matches if NOT followed by the pattern. /\d(?! dollars)/ matches digits not followed by “ dollars”.
  • (?<=...) Positive lookbehind — matches if preceded by the pattern. /(?<=\$)\d+/ matches digits after a dollar sign.
  • (?<!...) Negative lookbehind — matches if NOT preceded by the pattern.

Tips for Debugging Regex

  1. Test with a dedicated tester — Tools that highlight matches and show group captures in real time dramatically speed up development.
  2. Build incrementally — Start with a simple pattern that matches part of the input, then extend it step by step.
  3. Beware catastrophic backtracking — Patterns like (a+)+ can cause exponential time complexity. Use atomic groups or possessive quantifiers in engines that support them.
  4. Use named groups for readability(?<year>\d4)-(?<month>\d2) is self-documenting.
  5. Add comments with the x flag — Some regex engines support a verbose/x flag that ignores whitespace and allows inline comments for complex patterns.

Test your regex

Use our free Regex Tester to debug patterns against sample text in real time with match highlighting.