PHP Collection of Common Regular Expressions
- 2021-07-13 04:40:31
- OfStack
Regular expressions are often used to do websites at ordinary times. The following are some explanations and examples for your reference and modification only:
"^\d+$" // Non-negative integer (positive integer + 0 )
"^[0-9]*[1-9][0-9]*$" // Positive integer
"^((-\d+)|(0+))$" // Non-positive integer (negative integer + 0 )
"^-[0-9]*[1-9][0-9]*$" // Negative integer
"^-?\d+$" // Integer
"^\d+(\.\d+)?$" // Non-negative floating-point numbers (positive floating-point numbers) + 0 )
"^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$" // Positive floating point number
"^((-\d+(\.\d+)?)|(0+(\.0+)?))$" // Non-positive floating point number (negative floating point number) + 0 )
"^(-(([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
"^(-?\d+)(\.\d+)?$" // Floating point number
"^[A-Za-z]+$" // By 26 A string consisting of three English letters
"^[A-Z]+$" // By 26 A string consisting of uppercase English letters
"^[a-z]+$" // By 26 A string consisting of lowercase English letters
"^[A-Za-z0-9]+$" // By numbers and 26 A string consisting of three English letters
"^\w+$" // By numbers, 26 A string consisting of English letters or underscores
"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$" //email Address
"^[a-zA-z]+://(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\S*)?$" //url
/^(d{2}|d{4})-((0([1-9]{1}))|(1[1|2]))-(([0-2]([1-9]{1}))|(3[0|1]))$/ // Year - Month - Day
/^((0([1-9]{1}))|(1[1|2]))/(([0-2]([1-9]{1}))|(3[0|1]))/(d{2}|d{4})$/ // Month / Day / Year
"^([w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$" //Emil
/^((\+?[0-9]{2,4}\-[0-9]{3,4}\-)|([0-9]{3,4}\-))?([0-9]{7,8})(\-[0-9]+)?$/ // Telephone number
"^(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5])$" //IP Address
Regular expressions that match Chinese characters: [\u4e00-\u9fa5]
Matching double-byte characters ( Including Chinese characters ) : [^\x00-\xff]
A regular expression that matches a blank line: \n[\s| ]*\r
Matching HTML The regular expression of the tag: /<(.*)>.*<\/\1>|<(.*) \/>/
A regular expression that matches the first and last spaces: (^\s*)|(\s*$)
Matching Email Regular expression for address: \w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
Match URL URL Regular expression of: ^[a-zA-z]+://(\\w+(-\\w+)*)(\\.(\\w+(-\\w+)*))*(\\?\\S*)?$
Is the matching account legal ( Beginning with letters, allowing 5-16 Bytes, allowing alphanumeric underscores ) : ^[a-zA-Z][a-zA-Z0-9_]{4,15}$
Match domestic telephone number: (\d{3}-|\d{4}-)?(\d{8}|\d{7})?
Matching Tencent QQ No.: ^[1-9]*[1-9][0-9]*$
Metacharacters and their behavior in the context of regular expressions:
\ 将下1个字符标记为1个特殊字符、或1个原义字符、或1个后向引用、或1个8进制转义符。
^ 匹配输入字符串的开始位置。如果设置了 RegExp 对象的Multiline 属性,^ 也匹配 '\n' 或 '\r' 之后的位置。
$ 匹配输入字符串的结束位置。如果设置了 RegExp 对象的Multiline 属性,$ 也匹配 '\n' 或 '\r' 之前的位置。
* 匹配前面的子表达式零次或多次。
+ 匹配前面的子表达式1次或多次。+ 等价于 {1,}。
? 匹配前面的子表达式零次或1次。? 等价于 {0,1}。
{n} n 是1个非负整数,匹配确定的n 次。
{n,} n 是1个非负整数,至少匹配n 次。
{n,m} m 和 n 均为非负整数,其中n <= m。最少匹配 n 次且最多匹配 m 次。在逗号和两个数之间不能有空格。
? 当该字符紧跟在任何1个其他限制符 (*, +, ?, {n}, {n,}, {n,m}) 后面时,匹配模式是非贪婪的。非贪婪模式尽可能少的匹配所搜索的字符串,而默认的贪婪模式则尽可能多的匹配所搜索的字符串。
. 匹配除 "\n" 之外的任何单个字符。要匹配包括 '\n' 在内的任何字符,请使用象 '[.\n]' 的模式。
(pattern) 匹配pattern 并获取这1匹配。
(?:pattern) 匹配pattern 但不获取匹配结果,也就是说这是1个非获取匹配,不进行存储供以后使用。
(?=pattern) 正向预查,在任何匹配 pattern 的字符串开始处匹配查找字符串。这是1个非获取匹配,也就是说,该匹配不需要获取供以后使用。
(?!pattern) 负向预查,与(?=pattern)作用相反
x|y 匹配 x 或 y。
[xyz] 字符集合。
[^xyz] 负值字符集合。
[a-z] 字符范围,匹配指定范围内的任意字符。
[^a-z] 负值字符范围,匹配任何不在指定范围内的任意字符。
\b 匹配1个单词边界,也就是指单词和空格间的位置。
\B 匹配非单词边界。
\cx 匹配由x指明的控制字符。
\d 匹配1个数字字符。等价于 [0-9]。
\D 匹配1个非数字字符。等价于 [^0-9]。
\f 匹配1个换页符。等价于 \x0c 和 \cL。
\n 匹配1个换行符。等价于 \x0a 和 \cJ。
\r 匹配1个回车符。等价于 \x0d 和 \cM。
\s 匹配任何空白字符,包括空格、制表符、换页符等等。等价于[ \f\n\r\t\v]。
\S 匹配任何非空白字符。等价于 [^ \f\n\r\t\v]。
\t 匹配1个制表符。等价于 \x09 和 \cI。
\v 匹配1个垂直制表符。等价于 \x0b 和 \cK。
\w 匹配包括下划线的任何单词字符。等价于'[A-Za-z0-9_]'。
\W 匹配任何非单词字符。等价于 '[^A-Za-z0-9_]'。
\xn 匹配 n,其中 n 为106进制转义值。106进制转义值必须为确定的两个数字长。
\num 匹配 num,其中num是1个正整数。对所获取的匹配的引用。
\n 标识1个8进制转义值或1个后向引用。如果 \n 之前至少 n 个获取的子表达式,则 n 为后向引用。否则,如果 n 为8进制数字 (0-7),则 n 为1个8进制转义值。
\nm 标识1个8进制转义值或1个后向引用。如果 \nm 之前至少有is preceded by at least nm 个获取得子表达式,则 nm 为后向引用。如果 \nm 之前至少有 n 个获取,则 n 为1个后跟文字 m 的后向引用。如果前面的条件都不满足,若 n 和 m 均为8进制数字 (0-7),则 \nm 将匹配8进制转义值 nm。
\nml 如果 n 为8进制数字 (0-3),且 m 和 l 均为8进制数字 (0-7),则匹配8进制转义值 nml。
\un 匹配 n,其中 n 是1个用4个106进制数字表示的Unicode字符。
Regular expressions that match Chinese characters: [u4e00-u9fa5]
Matching double-byte characters ( Including Chinese characters ) : [^x00-xff]
A regular expression that matches a blank line: n[s| ]*r
Matching HTML The regular expression of the tag: /<(.*)>.*</1>|<(.*) />/
A regular expression that matches the first and last spaces: (^s*)|(s*$)
Matching Email Regular expression for address: w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*
Match URL URL Regular expression of: http://([w-]+.)+[w-]+(/[w- ./?%&=]*)?
Use regular expressions to restrict text box input in Web page forms:
Restrict Chinese input with regular expressions: onkeyup="value=value.replace(/[^u4E00-u9FA5]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^u4E00-u9FA5]/g,''))"
Restrict the entry of full-width characters with regular expressions: onkeyup="value=value.replace(/[^uFF00-uFFFF]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^uFF00-uFFFF]/g,''))"
Restrict the entry of only numbers with regular expressions: onkeyup="value=value.replace(/[^d]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^d]/g,''))"
Restrict the input of numbers and English only with regular expressions: onkeyup="value=value.replace(/[W]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^d]/g,''))"
= = = = = = = = = = = Common regular formula
Regular expressions that match Chinese characters: [\u4e00-\u9fa5]
Matching double-byte characters ( Including Chinese characters ) : [^\x00-\xff]
A regular expression that matches a blank line: \n[\s| ]*\r
Matching HTML The regular expression of the tag: /<(.*)>.*<\/\1>|<(.*) \/>/
A regular expression that matches the first and last spaces: (^\s*)|(\s*$)
Matching IP Regular expression for address: /(\d+)\.(\d+)\.(\d+)\.(\d+)/g //
Matching Email Regular expression for address: \w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
Match URL URL Regular expression of: http://(/[\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?
sql Statement: ^(select|drop|delete|create|update|insert).*$
Non-negative integer: ^\d+$
Positive integer: ^[0-9]*[1-9][0-9]*$
Non-positive integer: ^((-\d+)|(0+))$
Negative integer: ^-[0-9]*[1-9][0-9]*$
Integer: ^-?\d+$
Non-negative floating-point numbers: ^\d+(\.\d+)?$
Positive floating point number: ^((0-9)+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$
Non-positive floating point numbers: ^((-\d+\.\d+)?)|(0+(\.0+)?))$
Negative floating point number: ^(-(( Positive floating point number regular formula )))$
English string: ^[A-Za-z]+$
English capital string: ^[A-Z]+$
English lowercase string: ^[a-z]+$
English character numeric string: ^[A-Za-z0-9]+$
English numerals with underlined strings: ^\w+$
E-mail Address: ^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$
URL : ^[a-zA-Z]+://(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\s*)?$
Or: ^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$
Postal code: ^[1-9]\d{5}$
Chinese: ^[\u0391-\uFFE5]+$
Telephone number: ^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$
Mobile phone number: ^((\(\d{2,3}\))|(\d{3}\-))?13\d{9}$
Double byte character ( Including Chinese characters ) : ^\x00-\xff
Match the first and last spaces: (^\s*)|(\s*$) (Like vbscript That way trim Function)
Matching HTML Mark: <(.*)>.*<\/\1>|<(.*) \/>
Matching blank lines: \n[\s| ]*\r
Extract network links from information: (h|H)(r|R)(e|E)(f|F) *= *('|")?(\w|\\|\/|\.)+('|"| *|>)?
Extract the email address in the message: \w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
Picture links in extracted information: (s|S)(r|R)(c|C) *= *('|")?(\w|\\|\/|\.)+('|"| *|>)?
Extract the IP Address: (\d+)\.(\d+)\.(\d+)\.(\d+)
Extract the Chinese mobile phone number from the information: (86)*0*13\d{9}
China fixed telephone number in extracted information: (\(\d{3,4}\)|\d{3,4}-|\s)?\d{8}
Extract Chinese telephone numbers (including mobile and fixed telephones) from information: (\(\d{3,4}\)|\d{3,4}-|\s)?\d{7,14}
China Postal Code in Extract Information: [1-9]{1}(\d+){5}
Extract floating point numbers (that is, decimals) from information: (-?\d*)\.?\d+
Extract any number in the information : (-?\d*)(\.\d+)?
IP : (\d+)\.(\d+)\.(\d+)\.(\d+)
Telephone area code: /^0\d{2,3}$/
Tencent QQ No.: ^[1-9]*[1-9][0-9]*$
Account number ( Beginning with letters, allowing 5-16 Bytes, allowing alphanumeric underscores ) : ^[a-zA-Z][a-zA-Z0-9_]{4,15}$
Chinese, English, figures and underscores: ^[\u4e00-\u9fa5_a-zA-Z0-9]+$