Regular expressions are often used in our coding process. Correctly interpreting regular expressions is a very demanding task. Is there any way to use a machine to help us solve this problem? Here is a recommended website: https://regexper.com. The following is a set of screenshots of commonly used regular expression tests:
1. Digital Test
- number:
^[0-9]*$
- n-digit number:
^\d{n}$
such as:^\d{3}$
- At least n digits:
^\d{n,}$
such as:^\d{3,}$
- mn-digit numbers:
^\d{m,n}$
, such as:^\d{3,10}$
- Numbers starting with zero and non-zero:
^(0|[1-9][0-9]*)$
- Numbers with up to two decimal places and not starting with zero:
^([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 numbers with two decimal places:
^[0-9]+(.[0-9]{2})?$
- Positive real numbers with 1 to 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]*$
- A non-zero negative integer:
^\-[1-9][0-9]*$or^-[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 numbers:
^[1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0$
- Non-positive floating point number:
^((-\d+(\.\d+)?)|(0+(\.0+)?))$
or^(-([1-9]\d*\.\d*|0\.\d*[1-9]\d*))|0?\.0+|0$
- 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]*)))$
- Floating point number:
^(-?\d+)(\.\d+)?$
or^-?([1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0)$
2. Expression of check characters
- Chinese character:
^[\u4e00-\u9fa5]{0,}$
- English and numbers:
^[A-Za-z0-9]+$
or^[A-Za-z0-9]{4,40}$
- All characters with a length of 3-20:
^.{3,20}$
- A string consisting of 26 English letters:
^[A-Za-z]+$
- A string of 26 uppercase English letters:
^[A-Z]+$
- A string of 26 lowercase English letters:
^[a-z]+$
- A string consisting of numbers and 26 English letters:
^[A-Za-z0-9]+$
- A string consisting of numbers, 26 English letters, or underscores:
^\w+$
or^\w{3,20}$
- Chinese, English, numbers including underscores:
^[\u4E00-\u9FA5A-Za-z0-9_]+$
- Chinese, English, numbers but not including underscores and other symbols:
^[\u4E00-\u9FA5A-Za-z0-9_]+$
or^[\u4E00-\u9FA5A-Za-z0-9]{2,20}$
- Contains
^%&',;=?$\"
characters such as:[\^%&',;=?$\x22]+
- Does not contain characters containing ~:
[^~\x22]+
3. Expression of specific requirements
- 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-.\/?%&=]*)?$
- phone number:
^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$
- Telephone numbers (“XXX-XXXXXXX”, “XXXX-XXXXXXXX”, “XXX-XXXXXXX”, “XXX-XXXXXXXX”, “XXXXXXX”, and “XXXXXXXX”):
(\d{3,4}-)?\d{7,8}$
- Domestic telephone numbers (0511-4405222, 021-87888822):
\d{3}-\d{8}|\d{4}-\d{7}
- ID number (15 or 18 digits):
^(\d{15}|\d{18})$
- Short ID number (number, letter x at the end):
^([0-9]){7,18}(x|X)?$
or^\d{8,18}|[0-9x]{8,18}|[0-9X]{8,18}?$
- Is the account legal (starts with a letter, 5-16 bytes allowed, alphanumeric characters and underscores allowed):
^[a-zA-Z][a-zA-Z0-9_]{4,15}$
- Password (starts with a letter, has a length between 6 and 18 characters, and can only contain letters, numbers, and underscores):
^[a-zA-Z]\w{5,17}$
- Strong password (must contain a combination of uppercase and lowercase letters and numbers, no special characters, and a length between 8 and 10 characters):
^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$
- Date Format:
^\d{4}-\d{1,2}-\d{1,2}
- The 12 months of the year (01 to 09 and 1 to 12):
^(0?[1-9]|1[0-2])$
- 31 days of a month (01 to 09 and 1 to 31):
^((0?[1-9])|((1|2)[0-9])|30|31)$
- There are four forms of money that we can accept: “10000.00” and “10,000.00”, and “10000” and “10,000” without “cents”:
^[0-9]+(\.[0-9]+)?\$
- xml file:
^([a-zA-Z]+-?)+[a-zA-Z0-9]+\.[x|X][m|M][l|L]$
- Regular expression for Chinese characters:
[\u4e00-\u9fa5]
- Double-byte characters:
[^\x00-\xff]
(<including Chinese characters, which can be used to calculate the length of a string (a double-byte character counts as 2, and an ASCII character counts as 1)>) - Regular expression for blank lines:
\n\s*\r
(can be used to delete blank lines) - Regular expressions for HTML tags:
<(\S*?)[^>]*>.*?<\/\1>|<.*? \/>
- Regular expression for leading and trailing whitespace characters:
^\s*|\s*$
or(^\s*)|(\s*$)
(can be used to delete whitespace characters (including spaces, tabs, form feeds, etc.) at the beginning and end of a line, very useful expression) - Tencent QQ number:
[1-9][0-9]{4,}
(Tencent QQ number starts from 10000) - China Postal Code:
[1-9]\d{5}(?!\d)
(China Postal Code is 6 digits) - IP address:
\d+\.\d+\.\d+\.\d+
(useful when extracting IP address) - Extract annotations:
<!--(.*?)-->
I like it