Mastering Regex: Essential Patterns for Numbers, Text, and Special Formats
This guide compiles a comprehensive set of regular expression patterns for validating numbers, characters, emails, URLs, phone numbers, IP addresses, dates, and other special formats, providing ready-to-use code snippets that developers can apply across various programming contexts.
Numeric Validation Expressions
Digits: ^[0-9]*$
n-digit number: ^\d{n}$
At least n digits: ^\d{n,}$
m‑n digits: ^\d{m,n}$
Zero or non‑zero leading number: ^(0|[1-9][0-9]*)$
Non‑zero leading number with up to two decimal places: ^([1-9][0-9]*)+(.[0-9]{1,2})?$
Positive or negative number with 1‑2 decimal places: ^(\-)?\d+(\.\d{1,2})?$
Positive, negative, and decimal numbers: ^(\-|\+)?\d+(\.\d+)?$
Positive real number with two decimal places: ^[0-9]+(.[0-9]{2})?$
Positive real number with 1‑3 decimal places: ^[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$
Positive floating point: ^[1-9]\d*\.\d*|0\.\d*[1-9]\d*$ or ^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$
Negative floating point: ^-([1-9]\d*\.\d*|0\.\d*[1-9]\d*)$ or ^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$
Floating point: ^(-?\d+)(\.\d+)?$ or ^-?([1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0)$
Character Validation Expressions
Chinese characters: ^[\u4e00-\u9fa5]{0,}$
Alphanumeric: ^[A-Za-z0-9]+$ or ^[A-Za-z0-9]{4,40}$
Any characters length 3‑20: ^.{3,20}$
Only letters: ^[A-Za-z]+$
Only uppercase letters: ^[A-Z]+$
Only lowercase letters: ^[a-z]+$
Alphanumeric: ^[A-Za-z0-9]+$
Alphanumeric with underscore: ^\w+$ or ^\w{3,20}
Chinese, letters, digits, underscore: ^[\u4E00-\u9FA5A-Za-z0-9_]+$
Chinese, letters, digits (no underscore): ^[\u4E00-\u9FA5A-Za-z0-9]+$ or ^[\u4E00-\u9FA5A-Za-z0-9]{2,20}$
Allow special characters %&',;=?$": [^%&',;=?$\x22]+
Disallow tilde (~): [^~\x22]+
Special Requirement Expressions
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})+/.?
Internet URL: [a-zA-z]+://[^\s]* or ^http://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$
Mobile phone number (China): ^(13[0-9]|14[5|7]|15[0-9]|18[0-9])\d{8}$
Phone number formats (e.g., "XXX-XXXXXXX"): ^(\(\d{3,4}-)|\d{3.4}-)?\d{7,8}$
Domestic phone (e.g., 0511-4405222): \d{3}-\d{8}|\d{4}-\d{7}
Identity card number (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}?$
Account name (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 format (yyyy‑mm‑dd): ^\d{4}-\d{1,2}-\d{1,2}
Month (01‑12): ^(0?[1-9]|1[0-2])$
Day of month (01‑31): ^((0?[1-9])|((1|2)[0-9])|30|31)$
Other
.*matches any character except \n. /[\u4E00-\u9FA5]/ matches Chinese characters. /[\uFF00-\uFFFF]/ matches full‑width symbols. /[\u0000-\u00FF]/ matches half‑width symbols.
Money Input Formats
1. Accept four forms such as "10000.00", "10,000.00", "10000", "10,000": ^[1-9][0-9]*$ 2. Non‑zero leading number (zero alone allowed): ^(0|[1-9][0-9]*)$ 3. Optional leading minus sign: ^(0|-?[1-9][0-9]*)$ 4. Allow optional decimal part: ^[0-9]+(.[0-9]+)?$ 5. Require at least one digit after decimal: ^[0-9]+(.[0-9]{2})?$ 6. Allow one or two decimal digits: ^[0-9]+(.[0-9]{1,2})?$ 7. Allow commas as thousand separators: ^[0-9]{1,3}(,[0-9]{3})*(.[0-9]{1,2})?$ 8. Comma optional:
^([0-9]+|[0-9]{1,3}(,[0-9]{3})*)(.[0-9]{1,2})?$Note: The final result can replace + with * if empty strings are acceptable; remember to remove the escaping backslash when using the pattern.
PS
https://www.runoob.com/regexp/regexp-tutorial.html
Online regex tester: http://tool.oschina.net/regex/
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.
Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
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.
