Fundamentals 15 min read

Mastering Regex: Essential Patterns for Validating Numbers, Emails, URLs, and More

This article compiles a comprehensive collection of regular expressions for validating numeric formats, character sets, email addresses, domain names, URLs, phone numbers, IP addresses, dates, and other special patterns, providing developers with ready‑to‑use snippets for robust input validation.

Efficient Ops
Efficient Ops
Efficient Ops
Mastering Regex: Essential Patterns for Validating Numbers, Emails, URLs, and More

1. Numeric Validation Expressions

Digits:

^[0-9]*$

n‑digit number:

^\d{n}$

At least n digits:

^\d{n,}$

m‑n digits:

^\d{m,n}$

Zero or non‑zero leading number:

^(0|[1-9][0-9]*)$

Non‑zero number with up to two decimal places:

^([1-9][0-9]*)+(\.[0-9]{1,2})?$

Positive/negative number with 1‑2 decimal places:

^(\-)?\d+(\.\d{1,2})?$

Signed number with optional decimal:

^(\-|\+)?\d+(\.\d+)?$

Positive integer (non‑zero):

^[1-9]\d*$

or

^([1-9][0-9]*){1,3}$

or

^\+?[1-9][0-9]*$

Negative integer:

^\-[1-9]\d*$

Non‑negative integer:

^\d+$

or

^[1-9]\d*|0$

Non‑positive integer:

^-?[1-9]\d*|0$

or

^((\-\d+)|(0+))$

Non‑negative floating number:

^\d+(\.\d+)?$

or

^[1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0$

Non‑positive floating number:

^((\-\d+(\.\d+)?)|(0+(\.0+)?))$

or

^(-([1-9]\d*\.\d*|0\.\d*[1-9]\d*))|0?\.0+|0$

Positive floating number:

^[1-9]\d*\.\d*|0\.\d*[1-9]\d*$

or

^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$

Negative floating number:

^-([1-9]\d*\.\d*|0\.\d*[1-9]\d*)$

or

^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$

General floating number:

^(-?\d+)(\.\d+)?$

or

^-?([1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0)$

2. Character Validation Expressions

Chinese characters:

^[\u4e00-\u9fa5]{0,}$

Alphanumeric (letters and digits):

^[A-Za-z0-9]+$

or

^[A-Za-z0-9]{4,40}$

Any characters length 3‑20:

^.{3,20}$

Only letters (A‑Z, a‑z):

^[A-Za-z]+$

Only uppercase letters:

^[A-Z]+$

Only lowercase letters:

^[a-z]+$

Alphanumeric only:

^[A-Za-z0-9]+$

Alphanumeric with underscore:

^\w+$

or

^\w{3,20}$

Chinese, English, digits, underscore:

^[\u4E00-\u9FA5A-Za-z0-9_]+$

Chinese, English, digits (no underscore):

^[\u4E00-\u9FA5A-Za-z0-9]+$

or

^[\u4E00-\u9FA5A-Za-z0-9]{2,20}$

Allow specific punctuation (% & ', ; = ? $ "):

^[^%&',;=?$\x22]+

Disallow tilde (~):

[^~\x22]+

3. Special Requirement Expressions

Email address:

^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$

Domain name:

[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+\.?$

Internet URL:

[a-zA-z]+://[^\s]*

or

^http://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$

Mobile phone number (China):

^(13[0-9]|14[5|7]|15[0-9]|18[0-9])\d{8}$

Telephone number (various formats):

^(\(\d{3,4}-\)|\d{3,4}-)?\d{7,8}$

Chinese ID number (15 or 18 digits):

^\d{15}|\d{18}$

Short ID (digits, optional trailing x/X):

^([0-9]){7,18}(x|X)?$

or

^\d{8,18}|[0-9x]{8,18}|[0-9X]{8,18}?$

Account name (starts with letter, 5‑16 chars, letters/digits/underscore):

^[a-zA-Z][a-zA-Z0-9_]{4,15}$

Password (starts with letter, 6‑18 chars, letters/digits/underscore):

^[a-zA-Z]\w{5,17}$

Strong password (8‑10 chars, must include upper‑case, lower‑case, digit, no special chars):

^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$

Date (yyyy‑mm‑dd, leap‑year aware):

^\d{4}-\d{1,2}-\d{1,2}$

Month (01‑12 or 1‑12):

^(0?[1-9]|1[0-2])$

Day of month (01‑31):

^((0?[1-9])|((1|2)[0-9])|30|31)$

Money format (optional commas, optional 1‑2 decimal places):

^[0-9]{1,3}(,[0-9]{3})*(\.[0-9]{1,2})?$

XML file name:

^([a-zA-Z]+-?)+[a-zA-Z0-9]+\.[xX][mM][lL]$

Chinese character regex:

[\u4e00-\u9fa5]

Double‑byte character:

^\x00-\xff

Blank line removal:

\n\s*\r

HTML tag regex (simplified):

<(\S?)[^>]>.*?</\1>|<.*?\/>

Trim leading/trailing whitespace:

^\s|\s$

QQ number (starts with non‑zero, at least 5 digits):

[1-9][0-9]{4,}

Chinese postal code (6 digits):

[1-9]\d{5}(?!\d)

IPv4 address (basic):

\d+\.\d+\.\d+\.\d+

IPv4 address (full validation):

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

IPv6 address:

(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])))

Subnet mask:

((?:(?:25[0-5]|2[0-4]\d|[01]?\d?\d)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d?\d))
BackendProgrammingValidationregexpatterns
Efficient Ops
Written by

Efficient Ops

This public account is maintained by Xiaotianguo and friends, regularly publishing widely-read original technical articles. We focus on operations transformation and accompany you throughout your operations career, growing together happily.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.