[PHP] common regular expression collection

Time:2021-10-25
Regular expressions are used in string processing, form verification and other occasions, which are practical and efficient. Some commonly used expressions are collected here for future use. Article reprinted from: http://www.phpchina.com/31423/viewspace_ 9417.html

Regular expressions matching Chinese characters: [\u4e00-\u9fa5]
Comment: matching Chinese is really a headache. It’s easy to do with this expression

Match double byte characters (including Chinese characters):[^\x00-\xff]
Comment: can be used to calculate the length of a string (a double byte character length meter 2, ASCII character meter 1)

Regular expressions that match blank lines:\n\s*\r
Comment: can be used to delete blank lines

Regular expressions that match HTML Tags:<(\S*?)[^>]*>.*?</\1>|<.*? />
Comment: the version circulated on the Internet is too bad. The above one can only match the part. It is still powerless for complex nested tags

Regular expressions that match leading and trailing white space characters:^\s*|\s*$
Comment: it can be used to delete white space characters at the beginning and end of a line (including spaces, tabs, page breaks, etc.), which is a very useful expression

Regular expression matching email address:\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
Comment: form validation is useful

Regular expression matching URL of web address:[a-zA-z]+://[^\s]*
Commentary: the functions of the version circulated on the Internet are very limited, and the above can basically meet the needs

Whether the matching account number is legal (start with a letter, allow 5-16 bytes, allow alphanumeric underscores):^[a-zA-Z][a-zA-Z0-9_]{4,15}$
Comment: form validation is useful

Matching domestic telephone number:\d{3}-\d{8}|\d{4}-\d{7}
Comment: the matching form is 0511-4405222 or 021-87888822

Match Tencent QQ No.:[1-9][0-9]{4,}
Comment: Tencent QQ starts from 10000

Matching China Postal Code:[1-9]\d{5}(?!\d)
Commentary: the postal code of China is 6 digits

Matching ID card:\d{15}|\d{18}
Commentary: China’s ID card is 15 or 18

Matching IP address:\d+\.\d+\.\d+\.\d+
Comment: useful when extracting IP addresses

Match specific numbers:
^[1-9] \ d * $/ / match positive integer
^-[1-9] \ d * $/ / match negative integer
^-? [1-9] \ d * $/ / match integer
^[1-9] \ d * |0 $/ / match non negative integer (positive integer + 0)
^-[1-9] \ d * |0 $/ / match non positive integers (negative integers + 0)
^[1-9] \ d * \. \ d * |0 \. \ d * [1-9] \ d * $/ / match positive floating point numbers
^-([1-9] \ d * \. \ d * |0 \. \ d * [1-9] \ d *) $/ / match negative floating-point number
^-? ([1-9] \ d * \. \ d * |0 \. \ d * [1-9] \ d * |0? \. 0 + |0) $/ / matching floating-point numbers
^[1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\. 0 + |0 $/ / match non negative floating point number (positive floating point number + 0)
^(-([1-9]\d*\.\d*|0\.\d*[1-9]\d*))|0?\. 0 + |0 $/ / match non positive floating point numbers (negative floating point numbers + 0)
Commentary: it is useful when dealing with a large amount of data. Pay attention to correction in specific application

Match a specific string:
^[a-za-z] + $/ / matches a string of 26 English letters
^[A-Z] + $/ / matches a string composed of 26 uppercase letters
^[A-Z] + $/ / matches a string consisting of 26 lowercase letters
^[a-za-z0-9] + $/ / matches a string consisting of numbers and 26 English letters
^\W + $/ / matches a string consisting of numbers, 26 English letters, or underscores
Commentary: some of the most basic and commonly used expressions

Presentations on regular: http://soft.yesky.com/lesson/211/2350711.shtml