WebTool

Regular Expressions for Beginners: 20 Handy Patterns and Four Debugging Tricks

WebTool Team · Published 2026-09-04 · Regular Expressions / Regex / Development

A regular expression describes string-matching rules with a compact notation — about 10 metacharacters cover 80% of everyday needs. As you write a pattern, validate it live in our regex tester, which offers real-time highlighting and group extraction.

Metacharacter cheat sheet

Symbol Meaning Example
. Any character (except newline) a.c matches abc
\d \w \s Digit / word character / whitespace \d{4} matches a year
[abc] [^abc] Character set / negation [0-9a-f] hexadecimal
* + ? 0+ / 1+ / 0 or 1 times colou?r
{n,m} n to m times \d{6} postal code
^ $ Start / end of line ^# matches heading lines
() Group (capturing) (\d+)-(\d+)
(?:) Non-capturing group (?:https?://)
` ` Alternation

20 handy patterns

Use case Pattern
Mainland China mobile number ^1[3-9]\d{9}$
Email (lenient) ^[\w.+-]+@[\w-]+\.[\w.]+$
URL ^https?://[\w.-]+(?::\d+)?(?:/\S*)?$
IPv4 `^((25[0-5]
Date YYYY-MM-DD `^\d{4}-(0[1-9]
Chinese name ^[一-龥]{2,4}$
Chinese national ID (18 digits) ^\d{17}[\dXx]$
Strong password (8+ chars, upper/lower/digit) ^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$
Extract HTML tag contents <([a-z]+)[^>]*>(.*?)</\1>
Blank lines ^\s*$
Hex color ^#(?:[0-9a-fA-F]{3}){1,2}$
Base64 ^[A-Za-z0-9+/]+={0,2}$
Chinese punctuation [,。!?;:""''()]
Thousands-separated numbers \d{1,3}(?:,\d{3})+
Version number ^\d+\.\d+\.\d+$
File extension \.([a-z0-9]+)$
camelCase to words ([a-z])([A-Z])$1 $2
Trailing whitespace [ \t]+$
Repeated characters (.)\1{2,}
Comment lines (//) ^\s*//.*$

Four debugging tricks

  1. Greedy vs. lazy: .* consumes up to the last possible match, while .*? stops at the first. For quoted content, "[^"]*" is safer than ".*".
  2. Catastrophic backtracking (ReDoS): nested quantifiers like (a+)+ backtrack exponentially on hostile input — enforce an input length limit in online services.
  3. Don't forget anchors: add ^...$ to validate an entire string, otherwise \d+ happily matches inside "abc123".
  4. Escaping layers: when writing regex inside string literals (Java/Python), \d must be written as "\\d" — raw strings (Python r"") avoid this.

Last updated: 2026-09-04