HomeBlogRegex Patterns: 10 Essentials for Developers

Regex Patterns: 10 Essentials for Developers

Ten widely used regular expression patterns — email validation, URL extraction, log parsing and more — with line-by-line explanations.

DevKit is maintained by hcatree. I wrote the tools on this site and ran the compression benchmarks myself — the script and the raw data download from the benchmarks page.

2026-10-2511 min

Why Developers Need Regex

Regular expressions are a powerful tool for matching and manipulating text. While many developers find regex intimidating, mastering common patterns can significantly boost productivity — from form validation to log analysis, data cleaning to code refactoring, regex is everywhere.

The good news: you don't need to be a regex expert. In practice, 80% of scenarios require only about 10 core patterns. This article covers the most essential ones, each with detailed explanations and real-world examples.

Syntax Quick Reference

Before diving into specific patterns, let's review the core syntax elements:

Symbol Meaning Example
. Matches any character (except newline) a.c matches "abc", "axc"
\d Matches a digit \d{3} matches "123"
\w Matches word character \w+ matches "hello_42"
\s Matches whitespace a\sb matches "a b"
^ Matches start of string ^hello
$ Matches end of string world$
* Zero or more times ab*c matches "ac", "abc", "abbc"
+ One or more times ab+c matches "abc", "abbc"
? Zero or one time ab?c matches "ac", "abc"
{n,m} n to m times \d{2,4} matches 2-4 digits
[abc] Character set [aeiou] matches any vowel
(abc) Capture group (\d+) captures digit sequence

10 Essential Patterns

1. Email Address Validation

^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$

Explanation:

  • ^[a-zA-Z0-9._%+-]+ — Username part, allows letters, digits, and specific special characters
  • @ — Must have @ symbol
  • [a-zA-Z0-9.-]+ — Domain part
  • \.[a-zA-Z]{2,}$ — Top-level domain, at least 2 letters

Note: The full RFC 5322 email regex is extremely complex. This simplified version covers 99% of real scenarios. For strict validation, send a confirmation email instead.

2. URL Extraction

https?:\/\/[^\s"'<>]+

Explanation: Matches strings starting with http:// or https://, ending at whitespace or quotes. Perfect for extracting all links from text.

3. IPv4 Address Matching

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

Explanation: Precisely matches IPv4 addresses from 0.0.0.0 to 255.255.255.255. Each octet handles three ranges: 250-255, 200-249, and 0-199.

4. Date Format (YYYY-MM-DD)

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

Explanation: Matches standard ISO date format. Months limited to 01-12, days to 01-31. Note this doesn't validate actual days per month (Feb 30 would pass).

5. Phone Number (China Mainland)

^1[3-9]\d{9}$

Explanation: Matches Chinese 11-digit mobile numbers, starting with 1, second digit 3-9. Concise and efficient, covering all carrier prefixes.

6. HTML Tag Extraction

<([a-zA-Z][a-zA-Z0-9]*)\b[^>]*>(.*?)<\/\1>

Explanation:

  • <([a-zA-Z][a-zA-Z0-9]*) — Matches opening tag name (capture group 1)
  • \b[^>]*> — Matches tag attributes
  • (.*?) — Non-greedy match of tag content (capture group 2)
  • <\/\1> — Matches corresponding closing tag (backreference to group 1)

Note: Regex isn't suitable for parsing complex nested HTML. Use a DOM parser for production.

7. Password Strength Validation

^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$

Explanation: Uses lookahead assertions to ensure the password contains lowercase, uppercase, digits, and special characters, with minimum length of 8. Each (?=...) is an independent lookahead check.

8. Log Timestamp Extraction

\d{4}-\d{2}-\d{2}[T ]\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:?\d{2})?

Explanation: Matches ISO 8601 timestamps, compatible with variants that include/exclude milliseconds and timezone. Ideal for batch-extracting time information from log files.

9. File Path Parsing

^(?:[a-zA-Z]:\\|\/)?(?:[^\\\/]+[\\\/])*([^\\\/]+)$

Explanation: Compatible with both Windows (C:\path\file.txt) and Unix (/path/file.txt) path formats. Capture group 1 extracts the filename.

10. JSON Key Extraction

"([^"\\]*(?:\\.[^"\\]*)*)"(\s*:)/g

Explanation: Matches keys in JSON strings. Handles escaped characters within keys (e.g. "key\"name").

Practical Tips

Using Capture Groups to Extract Data

const regex = /(\d{4})-(\d{2})-(\d{2})/;
const match = "2026-10-25".match(regex);
// match[1] = "2026", match[2] = "10", match[3] = "25"

Non-Greedy Matching

// Greedy: matches to the last >
/<.+>/.exec('<b>bold</b> and <i>italic</i>')  // "<b>bold</b> and <i>italic</i>"

// Non-greedy: matches to the first >
/<.+?>/.exec('<b>bold</b> and <i>italic</i>')  // "<b>"

Named Capture Groups (More Readable)

const regex = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/;
const match = regex.exec("2026-10-25");
// match.groups.year = "2026"

Online Testing Tools

Real-time testing is essential when writing regex:

  • Use the Regex Tester to test regex matches live in your browser, with capture group inspection and flag toggling
  • All testing runs locally — your test data is never uploaded to any server

Regular expressions are a skill worth investing in. Master these 10 patterns and you'll handle the vast majority of text processing needs in daily development.

Discuss this articleEchoForum · no sign-up, replies in any language

Related Tools

Regex TesterTest regular expressions live