Fundamentals 9 min read

Common Regular Expressions for Validating Numbers, Characters, and Special Formats

This article provides a comprehensive collection of regular expression patterns for validating various numeric formats, character sets, and special input requirements such as email addresses, URLs, phone numbers, IDs, passwords, dates, and IP addresses, with clear examples for each case.

Java Captain
Java Captain
Java Captain
Common Regular Expressions for Validating Numbers, Characters, and Special Formats

Number Validation Expressions

Any digit (including empty): ^[0-9]*$

Exactly n digits: ^\d{n}$

At least n digits: ^\d{n,}$

Between m and n digits: ^\d{m,n}$

Zero or non‑zero leading digits: ^(0|[1-9][0-9]*)$

Non‑zero integer with up to two decimal places: ^([1-9][0-9]*)+(\.[0-9]{1,2})?$

Positive or negative number with 1‑2 decimal places: ^(-)?\d+(\.\d{1,2})$

Signed number with optional decimal part: ^(-|\+)?\d+(\.\d+)?$

Positive real number with exactly two decimal places: ^[0-9]+(\.[0-9]{2})?$

Positive real number with 1‑3 decimal places: ^[0-9]+(\.[0-9]{1,3})?$

Non‑zero positive integer: ^[1-9]\d*$ or ^([1-9][0-9]*){1,3}$ or ^\+?[1-9][0-9]*$

Non‑zero negative integer: ^-([1-9][0-9]*)$

Non‑negative integer (including zero): ^\d+$ or ^[1-9]\d*|0$

Non‑positive integer (including zero): ^-[1-9]\d*|0$ or ^((-\d+)|(0+))$

Non‑negative floating‑point number: ^\d+(\.\d+)?$ or complex alternatives

Non‑positive floating‑point number: ^((-\d+(\.\d+)?)|(0+(\.0+)?))$

Positive floating‑point number: ^[1-9]\d*\.\d*|0\.\d*[1-9]\d*$

Negative floating‑point number: ^-([1-9]\d*\.\d*|0\.\d*[1-9]\d*)$

General floating‑point number: ^(-?\d+)(\.\d+)?$

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 characters: ^[A-Za-z0-9]+$

Alphanumeric with underscore: ^\w+$ or ^\w{3,20}$

Chinese, letters, digits, underscore: ^[\u4E00-\u9FA5A-Za-z0-9_]+$

Chinese, letters, digits (no underscore): ^[\u4E00-\u9FA5A-Za-z0-9]+$ or ^[\u4E00-\u9FA5A-Za-z0-9]{2,20}$

Disallow tilde (~) character: [^~\x22]+

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[57]|15[0-35-9]|18[0-9])\d{8}$

Landline telephone (e.g., 0511-4405222): \d{3}-\d{8}|\d{4}-\d{7}

Comprehensive telephone regex (supports mobile, area code, extension): ((\d{11})|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$)

Chinese ID number (15 or 18 digits, last may be X): (^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)

Account name (starts with letter, 5‑16 characters, letters/digits/underscore): ^[a-zA-Z][a-zA-Z0-9_]{4,15}$

Password (starts with letter, 6‑18 characters, letters/digits/underscore): ^[a-zA-Z]\w{5,17}$

Strong password (8‑10 chars, must include upper, lower, digit, no special chars): ^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[a-zA-Z0-9]{8,10}$

Strong password (8‑10 chars, may include special chars): ^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$

Date format (YYYY‑M‑D): ^\d{4}-\d{1,2}-\d{1,2}

Month (01‑09 or 1‑12): ^(0?[1-9]|1[0-2])$

Day of month (01‑31): ^((0?[1-9])|([12][0-9])|30|31)$

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

Chinese character: [\u4e00-\u9fa5]

Blank line (for removal): \n\s*\r

HTML tag: <(\S*?)[^>]*>.*?|<.*?\s/\>

QQ number (starts from 10000): [1-9][0-9]{4,}

Chinese postal code (6 digits): [1-9]\d{5}(?!\d)

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

backendfrontendvalidationregular expressionsregexinput validationPattern Matching
Java Captain
Written by

Java Captain

Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.

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.