Fundamentals 12 min read

Essential Regular Expressions for Validating Numbers, Text, URLs and More

This article compiles a comprehensive collection of regular‑expression patterns for validating numeric formats, character sets, special requirements such as email, IP, dates, and various other input types, providing ready‑to‑use code snippets for developers.

Programmer DD
Programmer DD
Programmer DD
Essential Regular Expressions for Validating Numbers, Text, URLs and More

1. Number validation expressions

^[0-9]*$

– any number of digits (including empty). ^\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‑leading integer. ^([1-9][0-9]*)+(\.[0-9]{1,2})?$ – non‑zero‑leading integer with up to two decimal places. ^(\-)?\d+(\.\d{1,2})?$ – optional sign, integer or decimal with 1‑2 fractional digits. ^(\-|\+)?\d+(\.\d+)?$ – signed integer or decimal. ^[0-9]+(\.[0-9]{2})?$ – integer with optional two‑digit fraction. ^[0-9]+(\.[0-9]{1,3})?$ – integer with optional 1‑3 digit fraction. ^[1-9]\d*$ – positive integer without leading zero. ^\-[1-9]\d*$ – negative integer without leading zero. ^\d+$ – non‑negative integer. ^\-?[1-9]\d*$ – non‑positive integer. ^\d+(\.\d+)?$ – non‑negative floating number. ^\-?(\d+)(\.\d+)?$ – signed floating number.

2. Character validation expressions

^[\u4e00-\u9fa5]{0,}$

– Chinese characters. ^[A-Za-z0-9]+$ – alphanumeric. ^.{3,20}$ – any 3‑20 characters. ^[A-Za-z]+$ – letters only. ^[A-Z]+$ – uppercase letters. ^[a-z]+$ – lowercase letters. ^[A-Za-z0-9]+$ – letters and digits. ^\w+$ – word characters (letters, digits, underscore). ^[\u4E00-\u9FA5A-Za-z0-9_]+$ – Chinese, letters, digits, underscore. ^[\u4E00-\u9FA5A-Za-z0-9]+$ – Chinese, letters, digits (no underscore). [^%&',;=?$\x22]+ – excludes specific punctuation. [^~\x22]+ – excludes tilde.

3. 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-9]|18[0-9])\d{8}$ – Chinese mobile number. ^(\(\d{3,4}-)|\d{3.4}-)?\d{7,8}$ – telephone number. \d{3}-\d{8}|\d{4}-\d{7} – landline number. ^\d{15}|\d{18}$ – ID card number. ^[a-zA-Z][a-zA-Z0-9_]{4,15}$ – username (letter start, 5‑16 chars). ^[a-zA-Z]\w{5,17}$ – password (letter start, 6‑18 chars). ^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$ – strong password (mix of cases and digits). ^\d{4}-\d{1,2}-\d{1,2} – date format yyyy‑MM‑dd. ^(0?[1-9]|1[0-2])$ – month (01‑12). ^((0?[1-9])|((1|2)[0-9])|30|31)$ – day (01‑31).

4. Additional useful patterns

^([a-zA-Z]+-?)+[a-zA-Z0-9]+\.[xX][mM][lL]$

– XML file name. [\u4e00-\u9fa5] – single Chinese character. [^\x00-\xff] – double‑byte character. \n\s*\r – blank line. <(\S*?)[^>]*>.*?</\1>|<.*? /> – simple HTML tag. ^\s*|\s*$ – leading/trailing whitespace. [1-9][0-9]{4,} – QQ number. [1-9]\d{5}(?!\d) – Chinese postal code. \d+\.\d+\.\d+\.\d+ – IPv4 address.

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

– IPv4 validation.

((?:(?:25[0-5]|2[0-4]\d|[01]?\d?\d)\.){7,7}[0-9a-fA-F]{1,4}|...)

– IPv6 validation (truncated for brevity).

^((?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|...))$

– full date validation with leap‑year check. <!--(.*?)--> – HTML comment extraction. ^\s*[a-zA-Z\-]+\s*[:]{1}\s[a-zA-Z0-9\s.#]+[;]{1} – CSS property detection.

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.

validationregexdata validationregular expressioninput pattern
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

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.