Home > Backend Development > PHP Tutorial > A collection of commonly used regular expressions (numbers, characters, fixed formats)

A collection of commonly used regular expressions (numbers, characters, fixed formats)

高洛峰
Release: 2023-03-04 17:48:02
Original
1852 people have browsed it

Regular expression (regular expression) describes a string matching pattern, which can be used to check whether a string contains a certain substring, replace the matching substring, or extract from a string that meets a certain condition. substrings, etc.

When listing directories, *.txt in dir *.txt or ls *.txt is not a regular expression, because the meaning of * here is different from the * in regular expressions.

The method of constructing a regular expression is the same as that of creating a mathematical expression. That is, small expressions can be combined together to create larger expressions using a variety of metacharacters and operators. The components of a regular expression can be a single character, a collection of characters, a range of characters, a selection between characters, or any combination of all of these components.

Regular expressions are text patterns composed of ordinary characters (such as the characters a through z) and special characters (called "metacharacters"). A pattern describes one or more strings to match when searching for text. A regular expression acts as a template that matches a character pattern with a searched string.

The following is the editor's daily collection of some regular expressions commonly used in work. Commonly used regular verification expressions:

Number-related regular expressions

Number: ^ [0-9]*$

n-digit fixed-length number: ^\d{n}$

m-n-digit number: ^\d{ m,n}$

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

non-zero The number with up to two decimal places at the beginning: ^([1-9][0-9]*)+(.[0-9]{1,2})?$

with Positive or negative numbers with 1-2 decimal places: ^(\-)?\d+(\.\d{1,2})?$

Positive numbers, negative numbers, and decimals: ^ (\-|\+)?\d+(\.\d+)?$

Positive real number 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 integer: ^-[1-9]\d*|0$ Or ^((-\d+)|(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)$

Regular expressions related to characters

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

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

A string composed of numbers and 26 English letters: ^[A-Za-z0-9]+$

A string composed of numbers, 26 English letters or underscores String: ^\w+$ or ^\w{3,20}$

Chinese, English, numbers including underline: ^[\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 is legal (starting with a letter, 5-16 bytes allowed, alphanumeric underscores allowed): ^[a-zA-Z][a-zA- Z0-9_]{4,15}$

Password (starts with a letter, is between 6 and 18 in length, 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)(?=.*[a-z])(?=.*[A-Z]).{8,10}$

Fixed format conventional regular expression

Email address :^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$

Website 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 phone 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 number("XXX-XXXXXXX", "XXXX-XXXXXXXX", "XXX-XXXXXXX", "XXX-XXXXXXXX", "XXXXXXX" and "XXXXXXXX): ^(\(\d{3,4}-)| \d{3.4}-)?\d{7,8}$

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

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

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

12 months of the 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, currently the longest is 10 digits)

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))

The above is a collection of commonly used regular expressions introduced by the editor. (Numbers, characters, fixed formats), hope it helps everyone!

For more commonly used regular expressions (numbers, characters, fixed formats) related articles, please pay attention to the PHP Chinese website!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template