Common Regular Expressions for Validating Numbers, Characters, and Special Formats
This article provides a comprehensive collection of regular expression patterns for validating various numeric formats, character sets, and special input requirements such as email, URLs, phone numbers, dates, and IP addresses, offering developers ready-to-use examples for input validation tasks.
Numeric Validation Expressions – A series of regex patterns to match simple digits ( ^[0-9]*$ ), fixed‑length numbers ( ^\d{n}$ ), numbers with optional leading zeros ( ^(0|[1-9][0-9]*)$ ), positive/negative integers, decimals with specific precision (e.g., ^(\-)?\d+(\.\d{1,2})?$ ), and monetary formats allowing commas and optional cents (e.g., ^[0-9]{1,3}(,[0-9]{3})*(\.[0-9]{1,2})?$ ).
Character Validation Expressions – Regexes for Chinese characters ( ^[\u4e00-\u9fa5]{0,}$ ), alphanumeric strings ( ^[A-Za-z0-9]+$ ), specific length constraints ( ^.{3,20}$ ), uppercase or lowercase letters only ( ^[A-Z]+$ , ^[a-z]+$ ), and strings that may include underscores ( ^\w+$ ), as well as patterns that exclude certain symbols (e.g., [^%&',;=?$\x22]+ ).
Special‑Purpose Validation Expressions – Common patterns for email addresses ( ^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$ ), domain names, URLs, Chinese mobile numbers, telephone numbers, ID numbers, passwords (including strong password requirements ^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$ ), date formats with leap‑year handling, IP v4/v6 addresses, subnet masks, and HTML tag extraction. Additional utilities include whitespace line removal ( \n\s*\r ), comment extraction ( <!--(.*?)--> ), and file extension validation.
References for further learning are provided, such as the Runoob regex tutorial and online regex testing tools.
Java Captain
Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.
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.