Comprehensive Collection of Regular Expressions for Common Validation Scenarios
This article compiles a wide range of regular expressions for validating emails, phone numbers, IDs, dates, numbers, URLs, colors, file paths, and other common data formats, providing concise patterns and brief usage tips for developers and learners.
This article gathers a large set of regular expressions (regex) that can be used to validate various common data formats, such as email addresses, phone numbers, identification numbers, dates, numeric values, URLs, colors, file paths, and more. It also offers a quick tip to visualize regexes using the online tool jex.im/regulex .
Phone and Number Related
Examples include patterns for Chinese mobile numbers, landline numbers, email addresses, postal codes, ID numbers, bank card numbers, license plates, QQ numbers, WeChat IDs, version numbers, strong passwords, IP addresses (IPv4/IPv6), and URLs with optional ports.
<code>/^(?:(?:\+|00)86)?1\d{10}$/</code> <code>/^\d{3}-\d{8}|\d{4}-\d{7}$/</code> <code>/^\w+([-+.]*\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/</code> <code>/^[1-9]\d{5}(?:18|19|20)\d{2}(?:0[1-9]|1[0-2])(?:0[1-9]|[1-2]\d|30|31)\d{3}[\dXx]$/</code> <code>/^[1-9]\d{9,29}$/</code> <code>/^[\u4e00-\u9fa5]{0,}$/</code> <code>/^[0-9]{1,3}(?:\.[0-9]{1,3}){3}$/</code> <code>/^((ht|f)tps?:\/\/)?[\w-]+(\.[\w-]+)+(:\d{1,5})?\/?$/</code>Numeric Patterns
Patterns for pure digits, integers, positive/negative integers, floating‑point numbers, decimals, and specific length constraints.
<code>/^[0-9]*$/</code> <code>/^-?\d+$/</code> <code>/^\d+(?:\.\d+)?$/</code> <code>/^\d{5}$/</code>Character Patterns
Patterns for alphabetic strings, uppercase, lowercase, Chinese characters, full‑width/half‑width symbols, and mixed alphanumeric/underscore strings.
<code>/^[A-Za-z]+$/</code> <code>/^[A-Z]+$/</code> <code>/^[a-z]+$/</code> <code>/^[\u4e00-\u9fa5]+$/</code> <code>/^[\uFF00-\uFFFF]$/</code> <code>/^[\u0000-\u00FF]$/</code>Time and Date Patterns
Regular expressions for 24‑hour and 12‑hour time formats, various date formats (yyyy‑MM‑dd, yyyyMMdd, etc.), leap‑year checks, and combined date‑time strings.
<code>/^(?:[01]\d|2[0-3]):[0-5]\d:[0-5]\d$/</code> <code>/^\d{4}-\d{1,2}-\d{1,2}$/</code> <code>/^\d{8}$/</code> <code>/^(19|20)\d{2}$/</code>Programming‑Related Patterns
Regexes for hexadecimal colors, image/video URLs, MAC addresses, subnet masks, file paths (Linux/Windows), HTML comments/tags, CSS property extraction, and whitespace trimming.
<code>/^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/</code> <code>/^https?:\/\/.*\.(gif|png|jpg|jpeg|webp|svg|psd|bmp|tif)$/i</code> <code>/^((([a-f0-9]{2}:){5})|(([a-f0-9]{2}-){5}))[a-f0-9]{2}$/i</code> <code>/^\/\(\w+\/\)+\w+\.\w+$/</code> <code>/^[a-zA-Z]:\\(?:\w+\\)*\w+\.\w+$/</code>Life‑Related Patterns
Regexes for monetary values (with optional commas and decimals), passports, Hong Kong/Macau IDs, stock codes, and words that exclude a specific substring.
<code>/^-?\d+(,\d{3})*(\.\d{1,2})?$/</code> <code>/(^[1-9]{1}[0-9]*$)|(^[0-9]*\.[0-9]{2}$)/</code> <code>/(^[EeKkGgDdSsPpHh]\d{8}$)|(^(([Ee][a-fA-F])|([DdSsPp][Ee])|([Kk][Jj])|([Mm][Aa])|(1[45]))\d{7}$)/</code>Conclusion
The collection provides a handy reference for developers who need quick, ready‑to‑use regular expressions for a variety of validation tasks, and encourages readers to use the visualizer tool to better understand and customize these patterns.
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
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.