Commonly used regular expressions
I. Expression for verifying numbers
| use | Regular expressions | Example | illustrate |
|---|---|---|---|
| number | ^\d+$ | 12345 | Only non-negative integers (0 or positive integers) are allowed. |
| n-digit number | ^\d{n}$ | 1234 (n=4) | Exactly n digits |
| At least n digits | ^\d{n,}$ | 12345 (n=3) | At least n digits |
| mn digits | ^\d{m,n}$ | 1234 (m=2,n=4) | m to n digits |
| Zero and non-zero starting numbers | ^(0|[1-9]\d*)$ | 0, 123 | 0 or an integer starting with a non-zero value. |
| Non-zero numbers can have a maximum of two decimal places. | ^[1-9]\d*(\.\d{1,2})?$ | 12.34 | Positive integers or numbers with 1-2 decimal places |
| Positive and negative numbers with 1-2 decimal places | ^-?\d+(\.\d{1,2})?$ | -12.34, 56.7 | Positive and negative numbers, 1-2 decimal places |
| Positive numbers, negative numbers, decimals | ^[-+]?\d+(\.\d+)?$ | +12.3, -0.5 | Positive and negative integers or decimals |
| Two-decimal-positive real numbers | ^\d+(\.\d{2})?$ | 12, 12.34 | Positive integers or two decimal places |
| Positive real numbers with 1 to 3 decimal places | ^\d+(\.\d{1,3})?$ | 12.3, 12.345 | Positive integer or 1-3 decimal places |
| Non-zero positive integers | ^[1-9]\d*$ | 123 | Positive integers that do not contain 0 |
| Non-zero negative integers | ^-[1-9]\d*$ | -123 | Negative integers that do not contain 0 |
| non-negative integers | ^\d+$ | 0, 123 | 0 or a positive integer |
| non-positive integers | ^(-\d+|0)$ | -123, 0 | 0 or negative integer |
| nonnegative floating-point numbers | ^\d+(\.\d+)?$ | 0, 12.3 | 0 or positive floating-point number |
| Non-positive floating point numbers | ^(-\d+(\.\d+)?|0+(\.0+)?)$ | -12.3, 0, 0.0 | 0 or negative floating-point number |
| Positive floating point | ^(([1-9]\d*)|0)\.\d+$ | 0.1, 12.3 | Floating-point numbers greater than 0 |
| Negative score | ^-(([1-9]\d*)|0)\.\d+$ | -0.1, -12.3 | Floating-point numbers less than 0 |
| floating-point numbers | ^-?\d+(\.\d+)?$ | -12.3, 0, 45.6 | Positive and negative floating-point numbers |
Expressions for validating characters
| use | Regular expressions | Example | illustrate |
|---|---|---|---|
| English and numbers | ^[A-Za-z0-9]+$ | abc123 | Only English letters and numbers are allowed. |
| 4-40 characters of English letters and numbers | ^[A-Za-z0-9]{4.40}$ | abc12345 | 4-40 characters of English letters and numbers |
| All characters of length 3-20 | ^.{3,20}$ | abc, 12345678901234567890 | Any character, 3-20 positions |
| 26 letters of the English alphabet | ^[A-Z-z]+$ | abcXYZ | Only uppercase and lowercase English letters are allowed |
| 26 capital letters of the English alphabet | ^[A-Z]+$ | ABCXYZ | Only uppercase English letters are allowed |
| 26 lowercase English letters | ^[a-z]+$ | abcxyz | Lowercase English letters are allowed |
| Numbers and the 26 letters of the English alphabet | ^[A-Za-z0-9]+$ | abc123 | Only numbers and English letters are allowed |
| Numbers, letters, or underscores | ^\w+$ | abc_123 | Numbers, letters, or underscores |
| 3-20 digits, letters, or underscores | ^\w{3,20}$ | abc_123 | 3-20 digits, letters, or underscores |
| English letters, numbers, and underscores | ^[A-Za-z0-9_]+$ | abc_123 | English letters, numbers, and underscores are allowed. |
| English letters and numbers (excluding underscores) | ^[A-Za-z0-9]+$ | abc123 | English letters and numbers are allowed, excluding underscores. |
| You can enter content other than characters such as %&',;=?$". | ^[^%&',;=?$\x22]+$ | abc123!@# | Excluding characters such as %&',;=?$". |
| Do not enter characters containing ~ | ^[^~]+$ | abc123 | Excluding the ~ character |
III. Special Requirements Expressions
| use | Regular expressions | Example | illustrate |
|---|---|---|---|
| Email address | ^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$ | test@mail.com | Common email formats |
| domain name | [a-zA-Z0-9][-a-zA-Z0-9]{0.62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0.62})+\.? | www.example.com | Supports multi-level domains |
| Internet URL | ^[a-zA-Z]+://[^\s]+$ | https://abc.com | Common URL formats |
| legitimate account | ^[a-zA-Z][a-zA-Z0-9_]{4,15}$ | abc_123 | Beginning with a letter, 5-16 characters |
| Password (starts with a letter, 6-18 characters) | ^[a-zA-Z]\w{5,17}$ | a12345_ | Beginning with a letter, 6-18 characters |
| Strong password (no special characters) | ^(?=.*\d)(?=.*[az])(?=.*[AZ])[a-zA-Z0-9]{8,10}$ | Abc12345 | 8-10 characters, including uppercase and lowercase letters and numbers. |
| Strong password (may contain special characters) | ^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$ | Abc12345! | 8-10 characters, including uppercase and lowercase letters, numbers, and special characters. |
| Date format | ^\d{4}-\d{1,2}-\d{1,2}$ | 2023-01-01 | yyyy-mm-dd |
| 12 months a year | ^(0?[1-9]|1[0-2])$ | 01, 12 | 01-12 or 1-12 |
| 31 days in a month | ^((0?[1-9])|((1|2)[0-9])|30|31)$ | 01, 31 | 01-31 or 1-31 |
| Input money (integer) | ^(0|[1-9][0-9]*)$ | 10000 | non-negative integers |
| Input of money (negative numbers allowed) | ^(0|-?[1-9][0-9]*)$ | -10000 | Negative numbers are allowed |
| Money input (decimal) | ^[0-9]+(\.[0-9]{1,2})?$ | 10000.00 | Maximum two decimal places |
| Money input (with thousands separator) | ^([0-9]+|[0-9]{1,3}(,[0-9]{3})*)(\.[0-9]{1,2})?$ | 10,000.00 | Thousandths support |
| XML file | ^([a-zA-Z]+-?)+[a-zA-Z0-9]+\.[xX][mM][lL]$ | test-1.xml | XML filename |
| Double-byte characters | [^\x00-\xff] | Chinese characters, Japanese | Non-ASCII characters |
| blank line | ^\s*$ | (blank line) | Match blank lines |
| HTML tags | <[^>]+> | <div> | Simple HTML tags |
| Leading and trailing whitespace characters | ^\s*|\s*$ | abc | Remove leading and trailing whitespace |
| IPv4 address | ((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})(\.((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})){3} | 192.168.1.1 | IPv4 format |