Essential Regex Patterns for Numbers, Text, and Common Data Formats
This reference provides a comprehensive collection of regular expression patterns for validating numbers, characters, and various special formats such as email, URLs, IP addresses, dates, and passwords, offering ready-to-use Python regex snippets for developers.
Check Numbers
Number: ^[0-9]*$
n digits: ^\d{n}$
At least n digits: ^\d{n,}$
m~n digits: ^\d{m,n}$
Number starting with zero or non‑zero: ^(0|[1-9][0-9]*)$
Non‑zero start 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+)?$
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][]0-9"*$ or ^-[1-9]\d*$
Positive floating‑point number: ^[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 number: ^-([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]*)))$
Check Characters
English letters: ^[A-Za-z]+$
Any characters, length 3‑20: ^.{3,20}$
String of 26 letters: ^[A-Za-z]+$
Lowercase letters only: ^[a-z]+$
Alphanumeric characters: ^[A-Za-z0-9]+$
Alphanumeric or underscore: ^\w+$ or ^\w{3,20}
Match any character except newline: .* (excludes \n )
Special 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})+/.?
URL: [a-zA-z]+://[^\s]* or ^http://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$ or (?:(?:http://)|(?:https://))?(?:[\w](?:[\w\-]{0,61}[\w])?\.)+[a-zA-Z]{2,6}(?:/)
Phone number (various formats): ^(\(\d{3,4}-)|\d{3.4}-)?\d{7,8}$
Identity card number (15 or 18 digits): ^\d{15}|\d{18}$
Short ID (digits ending with optional x): ^([0-9]){7,18}(x|X)?$ or ^\d{8,18}|[0-9x]{8,18}|[0-9X]{8,18}?$
Account validity (starts with letter, 5‑16 characters, letters/digits/underscore): ^[a-zA-Z][a-zA-Z0-9_]{4,15}$
Password (starts with letter, 6‑18 characters, letters/digits/underscore): ^[a-zA-Z]\w{5,17}$
Strong password (8‑10 chars, includes upper, lower, digit, no special chars): ^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$
Date format: \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)$
XML file name: ^([a-zA-Z]+-?)+[a-zA-Z0-9]+\.[xX][mM][lL]$
IPv4 address: ((?:(?:25[0-5]|2[0-4]\d|[01]?\d?\d)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d?\d))
IPv6 address: (([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))
Subnet mask: ((?:(?:25[0-5]|2[0-4]\d|[01]?\d?\d)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d?\d))
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.
