Essential Regular Expressions for Common Data Validation
A comprehensive collection of regular expressions for validating numbers, characters, emails, URLs, phone numbers, dates, IP addresses, and other common data formats, presented with clear descriptions and ready-to-use patterns for developers.
Numeric Validation Patterns
Regular expressions for matching various numeric formats:
Any number of digits: ^[0-9]*$ Exactly n digits: ^\d{n}$ At least n digits: ^\d{n,}$ m to n digits: ^\d{m,n}$ Zero or non‑zero leading number: ^(0|[1-9][0-9]*)$ Non‑zero integer (max 2‑digit decimal): ^([1-9][0-9]*)+(\.[0-9]{1,2})?$ Signed number with optional 1‑2 decimal places: ^(\-)?\d+(\.\d{1,2})?$ Signed or unsigned number with optional decimal: ^(\-|\+)?\d+(\.\d+)?$ Positive real number with exactly 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]\d*$ 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 ^[1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0$ Non‑positive floating point: ^((\-\d+(\.\d+)?)|(0+(\.0+)?))$ or ^(-([1-9]\d*\.\d*|0\.\d*[1-9]\d*))|0?\.0+|0$ General floating point (signed): ^(-?\d+)(\.\d+)?$ or
^-?([1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0)$Character Validation Patterns
Regular expressions for validating various character sets:
Chinese characters (any length): ^[\u4e00-\u9fa5]{0,}$ Alphanumeric (letters and digits): ^[A-Za-z0-9]+$ or ^[A-Za-z0-9]{4,40}$ Any characters 3‑20 length: ^.{3,20}$ Only letters (A‑Z, a‑z): ^[A-Za-z]+$ Only uppercase letters: ^[A-Z]+$ Only lowercase letters: ^[a-z]+$ Alphanumeric (letters and digits): ^[A-Za-z0-9]+$ Alphanumeric with underscore: ^\w+$ or ^\w{3,20}$ Chinese, alphanumeric and underscore: ^[\u4E00-\u9FA5A-Za-z0-9_]+$ Chinese, alphanumeric without underscore: ^[\u4E00-\u9FA5A-Za-z0-9]+$ or ^[\u4E00-\u9FA5A-Za-z0-9]{2,20}$ Specific punctuation set: ^[%&',;=?$\"]+$ Exclude specific punctuation: [^%&',;=?$\x22]+ Exclude tilde:
[^~\x22]+Special Requirement Patterns
Regular expressions for common special formats:
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})+/.?URL (generic): [a-zA-z]+://[^\s]* or ^http://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$ Mobile phone (China): ^(13[0-9]|14[5|7]|15[0-9]|18[0-9])\d{8}$ Telephone (various formats): ^(\(\d{3,4}-)|\d{3.4}-)?\d{7,8}$ Landline (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}?$ Username (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 (yyyy‑mm‑dd, leap year aware):
^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$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 amount (allow commas, optional 1‑2 decimal places): ^[0-9]{1,3}(,[0-9]{3})*(\.[0-9]{1,2})?$ or ^([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 shortcut: [\u4e00-\u9fa5] Full‑width character range: [\uFF00-\uFFFF] Half‑width character range: [\u0000-\u00FF] Blank line removal: \n\s*\r HTML tag matching (simplified): <(\S?)[^>]>.*?(?</\1>|<.*?/>) Trim leading/trailing whitespace: ^\s|\s$ QQ number (starts at 10000): [1-9][0-9]{4,} Chinese postal code (6 digits): [1-9]\d{5}(?!\d) IPv4 address (basic): \d+\.\d+\.\d+\.\d+ IPv4 address (validated):
((?:(?: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}[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))CSS property extraction: ^\s*[a-zA-Z\-]+\s*[:]{1}\s[a-zA-Z0-9\s.#]+[;]{1} HTML comment extraction: <!--(.*?)--> Image source extraction: \\< *[img][^\\>]*[src] *= *[\"\']{0,1}([^\"\'\ >]*) Hex color code: ^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$ File extension validation (txt): ^([a-zA-Z]\:|\\)\\([^\\]+\\)*[^\/:*?"<>|]+\.txt(l)?$ IE version detection (5‑8):
^.*MSIE [5-8](?:\.[0-9]+)?(?!.*Trident\/[5-9]\.0).*$Notes
These patterns are intended as reference examples; they may need adaptation for specific programming languages or frameworks. Always test regexes with representative data to ensure correct behavior.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
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.
