Fundamentals 11 min read

Essential Regex Patterns for Validating Numbers, Text, and Common Data Formats

This article compiles a comprehensive set of regular expressions for validating numeric values, character strings, and various special formats such as emails, URLs, phone numbers, and monetary amounts, providing developers with ready‑to‑use patterns to streamline data validation tasks.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Essential Regex Patterns for Validating Numbers, Text, and Common Data Formats

Many developers struggle to find suitable regular expressions for data validation, so this collection gathers commonly used patterns for numbers, characters, and special requirements.

1. Number validation patterns

^[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 a non‑zero‑starting number ^([1-9][0-9]*)+(\.[0-9]{1,2})?$ – non‑zero start, 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})?$ – positive real number with exactly two decimals (optional) ^[0-9]+(\.[0-9]{1,3})?$ – positive real number with 1‑3 decimals (optional) ^[1-9]\d*$ – non‑zero positive integer ^-[1-9]\d*$ – non‑zero negative integer ^\d+$ – non‑negative integer (including zero) ^(-[1-9]\d*|0)$ – non‑positive integer ^\d+(\.\d+)?$ – non‑negative floating‑point number ^(-?\d+(\.\d+)?)$ – non‑positive floating‑point number ^[1-9]\d*\.\d*$ – positive floating‑point number ^-([1-9]\d*\.\d*|0\.\d+[1-9]\d*)$ – negative floating‑point number ^(-?\d+)(\.\d+)?$ – any floating‑point number

2. Character validation patterns

^[一-龥]{0,}$

– Chinese characters ^[A-Za-z0-9]+$ – letters and digits ^[A-Za-z0-9]{4,40}$ – 4‑40 alphanumeric characters ^.{3,20}$ – any characters, length 3‑20 ^[A-Za-z]+$ – only letters ^[A-Z]+$ – only uppercase letters ^[a-z]+$ – only lowercase letters ^[A-Za-z0-9]+$ – letters and digits ^\w+$ or ^\w{3,20}$ – letters, digits, underscore ^[一-龥A-Za-z0-9_]+$ – Chinese, letters, digits, underscore ^[一-龥A-Za-z0-9]+$ – Chinese, letters, digits (no underscore) [^%&',;=?$"]+ – allows special characters like ^%&',;=?$" [^~"]+ – disallows tilde (~)

3. Special‑purpose validation patterns

^\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]* or ^http://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$ – URL ^(13[0-9]|14[57]|15[0-9]|18[0-9])\d{8}$ – mobile phone number (China) ^(\d{3,4}-)?\d{7,8}$ – telephone number formats \d{3}-\d{8}|\d{4}-\d{7} – domestic telephone numbers ^\d{15}|\d{18}$ – ID card number (15 or 18 digits) ^([0-9]{7,18})(x|X)?$ – short ID number with optional x ^[a-zA-Z][a-zA-Z0-9_]{4,15}$ – valid account name (starts with letter, 5‑16 chars) ^[a-zA-Z]\w{5,17}$ – password (starts with letter, 6‑18 chars) ^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$ – strong password (mix of case and digits, 8‑10 chars) ^\d{4}-\d{1,2}-\d{1,2}$ – date format ^(0?[1-9]|1[0-2])$ – month (01‑12) ^((0?[1-9])|([12][0-9])|30|31)$ – day of month (01‑31)

Money formats: ^[1-9][0-9]*$ – non‑zero integer ^(0|[1-9][0-9]*)$ – zero or non‑zero integer ^(0|-?[1-9][0-9]*)$ – optional leading minus ^[0-9]+(\.[0-9]+)?$ – optional decimal part ^[0-9]+(\.[0-9]{2})?$ – exactly two decimal places ^[0-9]+(\.[0-9]{1,2})?$ – one or two decimal places ^[0-9]{1,3}(,[0-9]{3})*(\.[0-9]{1,2})?$ – commas as thousand separators ^([0-9]+|[0-9]{1,3}(,[0-9]{3})*)(\.[0-9]{1,2})?$ – flexible comma placement ^([a-zA-Z]+-?)+[a-zA-Z0-9]+\.[xX][mM][lL]$ – XML file name [一-龥] – single Chinese character [^-ÿ] – double‑byte characters (including Chinese) \s* – blank lines (useful for removal) <(\S*?)\b[^>]*>.*?</\1>|<.*?\/> – simple HTML tag matching ^\s*|\s*$ – leading/trailing whitespace [1-9][0-9]{4,} – QQ number (starts from 10000) [1-9]\d{5}(?!\d) – Chinese postal code (6 digits) \d+\.\d+\.\d+\.\d+ – basic IP address

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

– precise IPv4 pattern

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaScriptvalidationregular expressionsregexdata validationpatterns
MaGe Linux Operations
Written by

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.

0 followers
Reader feedback

How this landed with the community

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.