Comprehensive Collection of Regular Expressions for Data Validation
This article provides an extensive set of regular expression patterns for validating numbers, characters, and special formats such as email, URLs, phone numbers, dates, IP addresses, and more, offering developers ready-to-use solutions for common data validation tasks across various programming contexts.
1. Number Validation Expressions
Any number: ^[0-9]*$
Exact n digits: ^\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 start 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})?$
Positive, negative or decimal: ^(\-|\+)?\d+(\.\d+)?$
Positive real number with two decimals: ^[0-9]+(.[0-9]{2})?$
Positive real number with 1‑3 decimals: ^[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: ^\d+$ or ^[1-9]\d*|0$
Non‑positive integer: ^\-[1-9]\d*|0$ or ^((\-\d+)|(0+))$
Non‑negative floating point: ^\d+(\.\d+)?$ or complex form ^[1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0$
Non‑positive floating point: ^((\-\d+(\.\d+)?)|(0+(\.0+)?))$
Positive floating point: ^[1-9]\d*\.\d*|0\.\d*[1-9]\d*$ or alternative complex form
Negative floating point: ^-([1-9]\d*\.\d*|0\.\d*[1-9]\d*)$
General floating point: ^(-?\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: ^[A-Za-z0-9]+$ or ^[A-Za-z0-9]{4,40}$
Any characters length 3‑20: ^.{3,20}$
Only letters: ^[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, 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}$
Allow specific symbols: [^%&',;=?$\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 patterns: ^(\(\d{3,4}-)|\d{3.4}-)?\d{7,8}$
Domestic phone (e.g., 0511-4405222): \d{3}-\d{8}|\d{4}-\d{7}
Identity card (15 or 18 digits): ^\d{15}|\d{18}$
Short ID (digits, optional trailing x): ^([0-9]){7,18}(x|X)?$ or ^\d{8,18}|[0-9x]{8,18}|[0-9X]{8,18}?$
Account name (letter start, 5‑16 chars, letters/digits/underscore): ^[a-zA-Z][a-zA-Z0-9_]{4,15}$
Password (letter start, 6‑18 chars, letters/digits/underscore): ^[a-zA-Z]\w{5,17}$
Strong password (8‑10 chars, must include upper, lower, digit): ^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$
Date format (yyyy‑mm‑dd, leap year aware): ^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$
Month (01‑12): ^(0?[1-9]|1[0-2])$
Day of month (01‑31): ^((0?[1-9])|((1|2)[0-9])|30|31)$
Money formats (various): ^[1-9][0-9]*$ , ^(0|[1-9][0-9]*)$ , ^(0|-?[1-9][0-9]*)$ , ^[0-9]+(.[0-9]+)?$ , ^[0-9]+(.[0-9]{2})?$ , ^[0-9]+(.[0-9]{1,2})?$ , ^[0-9]{1,3}(,[0-9]{3})*(.[0-9]{1,2})?$ , ^([0-9]+|[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: \n\s*\r
HTML tag: <(\S*?)[^>]*>.*?</\1>|<.*? />
Trim leading/trailing whitespace: ^\s*|\s*$
QQ number: [1-9][0-9]{4,}
Chinese postal code: [1-9]\d{5}(?!\d)
IP address (simple): \d+\.\d+\.\d+\.\d+
IP‑v4 (full validation): \b(?:(?:25[0-5]|2[0-4]\d|[01]?\d?\d)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d?\d)\b
IP‑v6: (([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|...)
Subnet mask: ((?:(?:25[0-5]|2[0-4]\d|[01]?\d?\d)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d?\d))
Extract comments: <!--(.*?)-->
Extract CSS property: ^\s*[a-zA-Z\-]+\s*[:]{1}\s[a-zA-Z0-9\s.#]+[;]{1}
Extract hyperlinks (complex example): (<a\s*(?!.*\brel=)[^>]*)(href="https?:\/\/)((?!(?:(?:www\.)?'.implode('|(?:www\.)?', $follow_list).'))[^" rel="external nofollow" ]+)...
Extract image URLs: \\< *[img][^\\>]*[src] *= *[\"\']{0,1}([^\"\'\ >]*)
Extract color codes: ^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$
File extension validation (txt): ^([a-zA-Z]\:|\\)\\([^\\]+\\)*[^\/:*?"<>|]+\.txt(l)?$
IE version detection: ^.*MSIE [5-8](?:\.[0-9]+)?(?!.*Trident\/[5-9]\.0).*$
Note: The plus sign + can be replaced by the asterisk * if empty strings are also acceptable; remember to remove the escape character when using these patterns in code.
For further reference, see the online regex tutorial at Runoob and the regex testing tool at OSChina .
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.