Commonly used regular expressions:

Commonly used regular expressions:

Number related regular expressions

Numbers: ^[0-9]*$

Fixed-length n digits: ^\d{n}$

Numbers between mn digits: ^\d{m,n}$

Numbers starting with zero and non-zero: ^(0|[1-9][0-9]*)$

Non-zero leading numbers with up to two decimal places: ^([1-9][0-9]*)+(.[0-9]{1,2})?$

Positive or negative numbers 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})?$

Positive integer: ^\d+$ or ^[1-9]\d*|0$

Negative integers: ^-[1-9]\d*|0$ or ^((-\d+)|(0+))$

Positive float: ^[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 float: ^-([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 numbers: ^(-?\d+)(\.\d+)?$ or ^-?([1-9]\d*\.\d*|0\.\d*[1-9]\d *|0?\.0+|0)$

Character related regular expressions

Chinese characters: ^[\u4e00-\u9fa5]{0,}$

All characters of length 3-20: ^.{3,20}$

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 underscore: ^[\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}$

Whether the account number is legal (begins with a letter, allows 5-16 bytes, and allows alphanumeric underscores): ^[a-zA-Z][a-zA-Z0-9_]{4,15}$

Password (start with a letter, length between 6~18, 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, length between 8-10): ^(?=.*\d)(?=.*[az])(?=.* [AZ]).{8,10}$

Regular expressions with fixed format conventions

Email地址:^\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网址:^http://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$

Mobile number: ^(13[0-9]|17[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}$

Phone numbers ("XXX-XXXXXXX", "XXXX-XXXXXXXX", "XXX-XXXXXXX", "XXX-XXXXXXXX", "XXXXXXX" and "XXXXXXXX"): ^(\(\d{3,4}-)|\ d{3.4}-)?\d{7,8}$

Domestic phone numbers (0511-4405222, 021-87888822): \d{3}-\d{8}|\d{4}-\d{7}

ID number (15 digits, 18 digits): ^\d{15}|\d{18}$

Date format: ^\d{4}-\d{1,2}-\d{1,2}

12 months of a year (01~09 and 1~12): ^(0?[1-9]|1[0-2])$

31 days of a month (01~09 and 1~31): ^((0?[1-9])|((1|2)[0-9])|30|31)$

Tencent QQ number: [1-9][0-9]{4,8} (Tencent QQ number starts from 10000, and the current maximum number is 10)

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)

IP address: ((?:(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d)\\.){3}(?:25 [0-5]|2[0-4]\\d|[01]?\\d?\\d))

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325212126&siteId=291194637