← All Guides
🛠️ Developer Tools

Regular Expressions: A Practical Guide for Developers

8 min read · Updated June 2026

Regular expressions (regex) are a powerful pattern-matching language used in every programming language, text editor, and search tool. They can look intimidating, but once you understand the building blocks, you can read and write any regex.

The Building Blocks

Character Classes

PatternMatches
.Any character except newline
\dDigit [0-9]
\wWord character [a-zA-Z0-9_]
\sWhitespace (space, tab, newline)
[abc]Any of a, b, or c
[^abc]Anything except a, b, or c

Quantifiers

PatternMatchesType
*0 or moreGreedy
+1 or moreGreedy
?0 or 1Greedy
{n,m}Between n and mGreedy
*?0 or moreLazy
+?1 or moreLazy

Groups and References

  • (abc) — Capturing group (can be referenced as $1 or \1)
  • (?:abc) — Non-capturing group (doesn't create a reference)
  • (?<name>abc) — Named capturing group
  • abc|def — Alternation (matches abc OR def)

Flags

FlagNameEffect
gGlobalFind all matches, not just the first
iCase-insensitiveMatch regardless of case
mMultiline^ and $ match each line, not just the whole string
sDotall. matches newlines too
uUnicodeEnable Unicode property escapes (\p{L})
yStickyMatch only at lastIndex position

Common Patterns You'll Use

  • Email: /^[^\s@]+@[^\s@]+\.[^\s@]+$/
  • URL: /https?:\/\/(www\.)?[\w-]+\.[\w]{2,}(\/[\w-]*)*$/
  • Phone (US): /\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}/
  • IPv4: /\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/
  • Date (YYYY-MM-DD): /\d{4}-\d{2}-\d{2}/

🧪 Test your Regex Live

Use our Regex Tester & Highlighter to test any pattern with real-time match highlighting, capture group display, and all flag support.

The Bottom Line

  1. Regex is built from character classes, quantifiers, groups, and anchors
  2. Use lazy quantifiers (*? +?) to avoid over-matching
  3. Named groups (?<name>) make complex regex readable
  4. Always test with edge cases — regex bugs are subtle
  5. For complex parsing (HTML, CSV), use a proper parser, not regex

Disclaimer: This guide is for informational purposes only. Regex implementations vary slightly between languages — always test in your target environment.