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, IDs, and monetary amounts, providing developers with ready‑to‑use patterns to simplify data validation tasks.

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

1. Number validation regexes

^[0-9]*$

– matches any number of digits. ^\d{n}$ – matches exactly n digits. ^\d{n,}$ – matches at least n digits. ^\d{m,n}$ – matches between m and n digits. ^(0|[1-9][0-9]*)$ – matches zero or a non‑zero number without leading zeros. ^([1-9][0-9]*)+(\.[0-9]{1,2})?$ – non‑zero number with up to two decimal places. ^(-)?\d+(\.\d{1,2})?$ – positive or negative number with optional 1‑2 decimal places. ^(-|\+)?\d+(\.\d+)?$ – signed integer or decimal. ^[0-9]+(\.[0-9]{2})?$ – integer or number with exactly two decimal places. ^[0-9]+(\.[0-9]{1,3})?$ – integer or number with 1‑3 decimal places. ^[1-9]\d*$ – positive integer without leading zero. ^-[1-9]\d*$ – negative integer without leading zero. ^\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+)$ – negative floating‑point number. ^(-?\d+)(\.\d+)?$ – generic floating‑point number.

2. Character validation regexes

^[一-龥]{0,}$

– Chinese characters. ^[A-Za-z0-9]+$ – alphanumeric. ^[A-Za-z0-9]{4,40}$ – alphanumeric, length 4‑40. ^.{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+$ or ^\w{3,20}$ – word characters (letters, digits, underscore). ^[一-龥A-Za-z0-9_]+$ – Chinese, letters, digits, underscore. ^[一-龥A-Za-z0-9]+$ – Chinese, letters, digits (no underscore). [^%&',;=?$"]+ – allows characters except common special symbols. [^~"]+ – disallows the tilde character.

3. Special‑requirement regexes

^\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-3,5-9]|18[0-9])\d{8}$ – Chinese mobile phone number. ^(\d{3,4}-)?\d{7,8}$ – telephone number with optional area code. \d{3}-\d{8}|\d{4}-\d{7} – domestic telephone formats. ^\d{15}|\d{18}$ – ID card number (15 or 18 digits). ^([0-9]{7,18})(x|X)?$ – short ID with optional trailing X. ^[a-zA-Z][a-zA-Z0-9_]{4,15}$ – username starting with a letter, 5‑16 characters. ^[a-zA-Z]\w{5,17}$ – password starting with a letter, 6‑18 characters. ^(?=.*\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).

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]+)?$ – integer or decimal. ^[0-9]+(\.[0-9]{2})?$ – optional two‑decimal‑place fraction. ^[0-9]+(\.[0-9]{1,2})?$ – optional 1‑2 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 character (including Chinese). \s* – blank lines (useful for removal). <(\S*?)[^>]*>.*?</\1>|<.*? /> – simple HTML tag matcher. ^\s*|\s*$ – leading/trailing whitespace. [1-9][0-9]{4,} – QQ number (minimum 10000). [1-9]\d{5}(?!\d) – Chinese postal code. \d+\.\d+\.\d+\.\d+ – basic 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.

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.

Backendvalidationregular expressionsdata 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.