Essential Regular Expressions for Data Validation in Development
This article compiles a comprehensive set of commonly used regular expressions for validating numbers, characters, and special data formats such as emails, URLs, phone numbers, and monetary values, providing developers with a handy reference to streamline input validation tasks.
Many developers struggle to find reliable regular expressions for data validation; this collection gathers frequently used patterns for numbers, characters, and special requirements, serving as a quick reference.
Numeric Validation Expressions
^[0-9]*$– any number of digits ^\d{n}$ – exactly n digits ^\d{n,}$ – at least n digits ^\d{m,n}$ – between m and n digits ^(0|[1-9][0-9]*)$ – zero or non‑zero leading number ^([1-9][0-9]*)+(\.[0-9]{1,2})?$ – non‑zero integer with up to two decimal places ^(-)?\d+(\.\d{1,2})?$ – positive or negative number with 1‑2 decimal places ^(-|\+)?\d+(\.\d+)?$ – signed integer or decimal ^[0-9]+(\.[0-9]{2})?$ – number with exactly two decimal places (optional) ^[0-9]+(\.[0-9]{1,3})?$ – number with 1‑3 decimal places (optional) ^[1-9]\d*$ – positive integer without leading zero ^-[1-9]\d*$ – negative integer without leading zero ^\d+$ – non‑negative integer ^(-[1-9]\d*|0)$ – non‑positive integer ^\d+(\.\d+)?$ – non‑negative floating point ^(-\d+(\.\d+)?)$ – non‑positive floating point ^[1-9]\d*\.\d*$ – positive floating point ^-([1-9]\d*\.\d*)$ – negative floating point ^(-?\d+)(\.\d+)?$ – any floating point number
Character Validation Expressions
^[一-龥]{0,}$– Chinese characters ^[A-Za-z0-9]+$ – alphanumeric ^.{3,20}$ – any characters, length 3‑20 ^[A-Za-z]+$ – letters only ^[A-Z]+$ – uppercase letters only ^[a-z]+$ – lowercase letters only ^[A-Za-z0-9]+$ – letters and digits ^\w+$ – letters, digits, underscore ^[一-龥A-Za-z0-9_]+$ – Chinese, letters, digits, underscore ^[一-龥A-Za-z0-9]+$ – Chinese, letters, digits (no underscore) [^%&',;=?$"]+ – allows special symbols like ^%&',;=?$" [^~"]+ – disallows tilde (~)
Special‑Requirement Expressions
^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$– email address
[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+\.?$– domain name [a-zA-Z]+://[^\s]* – generic URL ^(13[0-9]|14[57]|15[0-35-9]|18[0-9])\d{8}$ – Chinese mobile number ^(\d{3,4}-)?\d{7,8}$ – telephone number ^\d{3}-\d{8}|\d{4}-\d{7}$ – domestic telephone formats ^\d{15}|\d{18}$ – ID card number ^[0-9]{7,18}(x|X)?$ – short ID with optional X ^[a-zA-Z][a-zA-Z0-9_]{4,15}$ – valid account name ^[a-zA-Z]\w{5,17}$ – password starting with a letter, 6‑18 chars ^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$ – strong password (letters and digits, 8‑10 chars) ^\d{4}-\d{1,2}-\d{1,2}$ – date format YYYY‑M‑D ^(0?[1-9]|1[0-2])$ – month (01‑12) ^((0?[1-9])|([12][0-9])|30|31)$ – day of month (01‑31) ^[1-9][0-9]*$ – monetary amount without leading zero ^(0|[1-9][0-9]*)$ – zero or non‑zero amount ^(0|-?[1-9][0-9]*)$ – optional negative amount ^[0-9]+(\.[0-9]+)?$ – amount with optional decimal ^[0-9]+(\.[0-9]{2})?$ – amount with exactly two decimal places ^[0-9]+(\.[0-9]{1,2})?$ – amount with 1‑2 decimal places ^[0-9]{1,3}(,[0-9]{3})*(\.[0-9]{1,2})?$ – amount with commas and optional decimals ^([0-9]+|[0-9]{1,3}(,[0-9]{3})*)(\.[0-9]{1,2})?$ – flexible comma‑separated amount ^([a-zA-Z]+-?)+[a-zA-Z0-9]+\.[xX][mM][lL]$ – XML file name [一-龥] – single Chinese character [^-ÿ] – double‑byte character (including Chinese) \s* – blank line (useful for removal) <(\S*?)[^>]*>.*?</\1>|<.*? /> – simple HTML tag matcher ^\s*|\s*$ – trim leading/trailing whitespace [1-9][0-9]{4,} – Tencent QQ number (minimum 10000) [1-9]\d{5}(?!\d) – Chinese postal code (6 digits) \d+\.\d+\.\d+\.\d+ – IP address pattern
((?:(?:25[0-5]|2[0-4]\d|[01]?\d?\d)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d?\d))– precise IPv4 matcher
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.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
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.
