Home Backend Development PHP Tutorial PHP惯用正则表达式汇总

PHP惯用正则表达式汇总

Jun 13, 2016 pm 01:21 PM
pattern quot replace

PHP常用正则表达式汇总

1. 平时做网站经常要用正则表达式,下面是一些讲解和例子,仅供大家参考和修改使用:
2. "^\d+$"  //非负整数(正整数 + 0)
3. "^[0-9]*[1-9][0-9]*$"  //正整数
4. "^((-\d+)|(0+))$"  //非正整数(负整数 + 0)
5. "^-[0-9]*[1-9][0-9]*$"  //负整数
6. "^-?\d+$"    //整数
7. "^\d+(\.\d+)?$"  //非负浮点数(正浮点数 + 0)
8. "^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$"  //正浮点数
9. "^((-\d+(\.\d+)?)|(0+(\.0+)?))$"  //非正浮点数(负浮点数 + 0)
10. "^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$"  //负浮点数
11. "^(-?\d+)(\.\d+)?$"  //浮点数
12. "^[A-Za-z]+$"  //由26个英文字母组成的字符串
13. "^[A-Z]+$"  //由26个英文字母的大写组成的字符串
14. "^[a-z]+$"  //由26个英文字母的小写组成的字符串
15. "^[A-Za-z0-9]+$"  //由数字和26个英文字母组成的字符串
16. "^\w+$"  //由数字、26个英文字母或者下划线组成的字符串
17. "^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$"    //email地址
18. "^[a-zA-z]+://(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\S*)?$"  //url
19. /^(d{2}|d{4})-((0([1-9]{1}))|(1[1|2]))-(([0-2]([1-9]{1}))|(3[0|1]))$/ // 年-月-日
20. /^((0([1-9]{1}))|(1[1|2]))/(([0-2]([1-9]{1}))|(3[0|1]))/(d{2}|d{4})$/ // 月/日/年
21. "^([w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$" //Emil
22. /^((\+?[0-9]{2,4}\-[0-9]{3,4}\-)|([0-9]{3,4}\-))?([0-9]{7,8})(\-[0-9]+)?$/ //电话号码
23. "^(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地址
24.
25. 匹配中文字符的正则表达式: [\u4e00-\u9fa5]
26. 匹配双字节字符(包括汉字在内):[^\x00-\xff]
27. 匹配空行的正则表达式:\n[\s| ]*\r
28. 匹配HTML标记的正则表达式:/.*|/
29. 匹配首尾空格的正则表达式:(^\s*)|(\s*$)
30. 匹配Email地址的正则表达式:\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
31. 匹配网址URL的正则表达式:^[a-zA-z]+://(\\w+(-\\w+)*)(\\.(\\w+(-\\w+)*))*(\\?\\S*)?$
32. 匹配帐号是否合法(字母开头,允许5-16字节,允许字母数字下划线):^[a-zA-Z][a-zA-Z0-9_]{4,15}$
33. 匹配国内电话号码:(\d{3}-|\d{4}-)?(\d{8}|\d{7})?
34. 匹配腾讯QQ号:^[1-9]*[1-9][0-9]*$
35.
36.
37. 元字符及其在正则表达式上下文中的行为:
38.
39. \ 将下一个字符标记为一个特殊字符、或一个原义字符、或一个后向引用、或一个八进制转义符。
40.
41. ^ 匹配输入字符串的开始位置。如果设置了 RegExp 对象的Multiline 属性,^ 也匹配 ’\n’ 或 ’\r’ 之后的位置。
42.
43. $ 匹配输入字符串的结束位置。如果设置了 RegExp 对象的Multiline 属性,$ 也匹配 ’\n’ 或 ’\r’ 之前的位置。
44.
45. * 匹配前面的子表达式零次或多次。
46.
47. + 匹配前面的子表达式一次或多次。+ 等价于 {1,}。
48.
49. ? 匹配前面的子表达式零次或一次。? 等价于 {0,1}。
50.
51. {n} n 是一个非负整数,匹配确定的n 次。
52.
53. {n,} n 是一个非负整数,至少匹配n 次。
54.
55. {n,m} m 和 n 均为非负整数,其中n
56.
57. ? 当该字符紧跟在任何一个其他限制符 (*, +, ?, {n}, {n,}, {n,m}) 后面时,匹配模式是非贪婪的。非贪婪模式尽可能少的匹配所搜索的字符串,而默认的贪婪模式则尽可能多的匹配所搜索的字符串。
58.
59. . 匹配除 "\n" 之外的任何单个字符。要匹配包括 ’\n’ 在内的任何字符,请使用象 ’[.\n]’ 的模式。
60. (pattern) 匹配pattern 并获取这一匹配。
61.
62. (?:pattern) 匹配pattern 但不获取匹配结果,也就是说这是一个非获取匹配,不进行存储供以后使用。
63.
64. (?=pattern) 正向预查,在任何匹配 pattern 的字符串开始处匹配查找字符串。这是一个非获取匹配,也就是说,该匹配不需要获取供以后使用。
65.
66. (?!pattern) 负向预查,与(?=pattern)作用相反
67.
68. x|y 匹配 x 或 y。
69.
70. [xyz] 字符集合。
71.
72. [^xyz] 负值字符集合。
73.
74. [a-z] 字符范围,匹配指定范围内的任意字符。
75.
76. [^a-z] 负值字符范围,匹配任何不在指定范围内的任意字符。
77.
78. \b 匹配一个单词边界,也就是指单词和空格间的位置。
79.
80. \B 匹配非单词边界。
81.
82. \cx 匹配由x指明的控制字符。
83.
84. \d 匹配一个数字字符。等价于 [0-9]。
85.
86. \D 匹配一个非数字字符。等价于 [^0-9]。
87.
88. \f 匹配一个换页符。等价于 \x0c 和 \cL。
89.
90. \n 匹配一个换行符。等价于 \x0a 和 \cJ。
91.
92. \r 匹配一个回车符。等价于 \x0d 和 \cM。
93.
94. \s 匹配任何空白字符,包括空格、制表符、换页符等等。等价于[ \f\n\r\t\v]。
95.
96. \S 匹配任何非空白字符。等价于 [^ \f\n\r\t\v]。
97.
98. \t 匹配一个制表符。等价于 \x09 和 \cI。
99.
100. \v 匹配一个垂直制表符。等价于 \x0b 和 \cK。
101.
102. \w 匹配包括下划线的任何单词字符。等价于’[A-Za-z0-9_]’。
103.
104. \W 匹配任何非单词字符。等价于 ’[^A-Za-z0-9_]’。
105.
106. \xn 匹配 n,其中 n 为十六进制转义值。十六进制转义值必须为确定的两个数字长。
107.
108. \num 匹配 num,其中num是一个正整数。对所获取的匹配的引用。
109.
110. \n 标识一个八进制转义值或一个后向引用。如果 \n 之前至少 n 个获取的子表达式,则 n 为后向引用。否则,如果 n 为八进制数字 (0-7),则 n 为一个八进制转义值。
111.
112. \nm 标识一个八进制转义值或一个后向引用。如果 \nm 之前至少有is preceded by at least nm 个获取得子表达式,则 nm 为后向引用。如果 \nm 之前至少有 n 个获取,则 n 为一个后跟文字 m 的后向引用。如果前面的条件都不满足,若 n 和 m 均为八进制数字 (0-7),则 \nm 将匹配八进制转义值 nm。
113.
114. \nml 如果 n 为八进制数字 (0-3),且 m 和 l 均为八进制数字 (0-7),则匹配八进制转义值 nml。
115.
116. \un 匹配 n,其中 n 是一个用四个十六进制数字表示的Unicode字符。
117.
118. 匹配中文字符的正则表达式: [u4e00-u9fa5]
119.
120. 匹配双字节字符(包括汉字在内):[^x00-xff]
121.
122. 匹配空行的正则表达式:n[s| ]*r
123.
124. 匹配HTML标记的正则表达式:/.*1>|/
125.
126. 匹配首尾空格的正则表达式:(^s*)|(s*$)
127.
128. 匹配Email地址的正则表达式:w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*
129.
130. 匹配网址URL的正则表达式:http://([w-]+.)+[w-]+(/[w- ./?%&=]*)?
131.
132. 利用正则表达式限制网页表单里的文本框输入内容:
133.
134. 用正则表达式限制只能输入中文:onkeyup="value=value.replace(/[^u4E00-u9FA5]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^u4E00-u9FA5]/g,''))"
135.
136. 用正则表达式限制只能输入全角字符: onkeyup="value=value.replace(/[^uFF00-uFFFF]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^uFF00-uFFFF]/g,''))"
137.
138. 用正则表达式限制只能输入数字:onkeyup="value=value.replace(/[^d]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^d]/g,''))"
139.
140. 用正则表达式限制只能输入数字和英文:onkeyup="value=value.replace(/[W]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^d]/g,''))"
141.
142. =========常用正则式
143.
144.
145.
146. 匹配中文字符的正则表达式: [\u4e00-\u9fa5]
147.
148. 匹配双字节字符(包括汉字在内):[^\x00-\xff]
149.
150. 匹配空行的正则表达式:\n[\s| ]*\r
151.
152. 匹配HTML标记的正则表达式:/.*|/
153.
154. 匹配首尾空格的正则表达式:(^\s*)|(\s*$)
155.
156. 匹配IP地址的正则表达式:/(\d+)\.(\d+)\.(\d+)\.(\d+)/g //
157.
158. 匹配Email地址的正则表达式:\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
159.
160. 匹配网址URL的正则表达式:http://(/[\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?
161.
162. sql语句:^(select|drop|delete|create|update|insert).*$
163.
164. 1、非负整数:^\d+$
165.
166. 2、正整数:^[0-9]*[1-9][0-9]*$
167.
168. 3、非正整数:^((-\d+)|(0+))$
169.
170. 4、负整数:^-[0-9]*[1-9][0-9]*$
171.
172. 5、整数:^-?\d+$
173.
174. 6、非负浮点数:^\d+(\.\d+)?$
175.
176. 7、正浮点数:^((0-9)+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$
177.
178. 8、非正浮点数:^((-\d+\.\d+)?)|(0+(\.0+)?))$
179.
180. 9、负浮点数:^(-((正浮点数正则式)))$
181.
182. 10、英文字符串:^[A-Za-z]+$
183.
184. 11、英文大写串:^[A-Z]+$
185.
186. 12、英文小写串:^[a-z]+$
187.
188. 13、英文字符数字串:^[A-Za-z0-9]+$
189.
190. 14、英数字加下划线串:^\w+$
191.
192. 15、E-mail地址:^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$
193.
194. 16、URL:^[a-zA-Z]+://(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\s*)?$
195. 或:^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^\"\"])*$
196.
197. 17、邮政编码:^[1-9]\d{5}$
198.
199. 18、中文:^[\u0391-\uFFE5]+$
200.
201. 19、电话号码:^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$
202.
203. 20、手机号码:^((\(\d{2,3}\))|(\d{3}\-))?13\d{9}$
204.
205. 21、双字节字符(包括汉字在内):^\x00-\xff
206.
207. 22、匹配首尾空格:(^\s*)|(\s*$)(像vbscript那样的trim函数)
208.
209. 23、匹配HTML标记:.*|
210.
211. 24、匹配空行:\n[\s| ]*\r
212.
213. 25、提取信息中的网络链接:(h|H)(r|R)(e|E)(f|F) *= *('|")?(\w|\\|\/|\.)+('|"| *|>)?
214.
215. 26、提取信息中的邮件地址:\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
216.
217. 27、提取信息中的图片链接:(s|S)(r|R)(c|C) *= *('|")?(\w|\\|\/|\.)+('|"| *|>)?
218.
219. 28、提取信息中的IP地址:(\d+)\.(\d+)\.(\d+)\.(\d+)
220.
221. 29、提取信息中的中国手机号码:(86)*0*13\d{9}
222.
223. 30、提取信息中的中国固定电话号码:(\(\d{3,4}\)|\d{3,4}-|\s)?\d{8}
224.
225. 31、提取信息中的中国电话号码(包括移动和固定电话):(\(\d{3,4}\)|\d{3,4}-|\s)?\d{7,14}
226.
227. 32、提取信息中的中国邮政编码:[1-9]{1}(\d+){5}
228.
229. 33、提取信息中的浮点数(即小数):(-?\d*)\.?\d+
230.
231. 34、提取信息中的任何数字 :(-?\d*)(\.\d+)?
232.
233. 35、IP:(\d+)\.(\d+)\.(\d+)\.(\d+)
234.
235. 36、电话区号:/^0\d{2,3}$/
236.
237. 37、腾讯QQ号:^[1-9]*[1-9][0-9]*$
238.
239. 38、帐号(字母开头,允许5-16字节,允许字母数字下划线):^[a-zA-Z][a-zA-Z0-9_]{4,15}$
240.
241. 39、中文、英文、数字及下划线:^[\u4e00-\u9fa5_a-zA-Z0-9]+$
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use the REPLACE function to replace a specified part of a string in MySQL How to use the REPLACE function to replace a specified part of a string in MySQL Jul 25, 2023 pm 01:18 PM

MySQL is a commonly used relational database management system that provides a variety of functions to process and operate data. Among them, the REPLACE function is used to replace the specified part of the string. In this article, we will introduce how to use the REPLACE function for string replacement in MySQL and demonstrate its usage through code examples. First, let’s take a look at the syntax of the REPLACE function: REPLACE(str,search_str,replace_str).

What are the string search and replace techniques in Python? What are the string search and replace techniques in Python? Oct 20, 2023 am 11:42 AM

What are the string search and replace techniques in Python? (Specific code example) In Python, strings are a common data type, and we often encounter string search and replace operations in daily programming. This article will introduce some common string search and replacement techniques, accompanied by specific code examples. To find a specific substring in a string, you can use the find() method or index() method of the string. The find() method returns the index of the first occurrence of the substring in the string.

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

Using Pattern.compile method in Java Using Pattern.compile method in Java Feb 18, 2024 pm 09:04 PM

Usage of Pattern.compile function in Java The Pattern.compile function in Java is a method used to compile regular expressions. Regular expression is a powerful string matching and processing tool that can be used to find, replace, verify strings and other operations. The Pattern.compile function allows us to compile a string pattern into a Pattern object, which can then be used to perform a series of string operations. Pattern.compi

PatternSyntaxException class in Java regular expressions PatternSyntaxException class in Java regular expressions Sep 11, 2023 pm 07:37 PM

The PatternSyntaxException class represents an unchecked exception thrown when a syntax error occurs in a regular expression string. This class contains three main methods namely - getDescription() - returns the description of the error. getIndex() - Returns the error index. getPattern() - Returns the regular expression pattern in which the error occurred. getMessage() - Returns the complete message containing the error, the index, the regular expression pattern in which the error occurred, and the error in the indicated pattern. Example Real-time demonstration importjava.util.Scanner;importjava.util.regex.Matcher;i

Use the replace() method of the StringBuilder class in Java to replace part of the content in a string Use the replace() method of the StringBuilder class in Java to replace part of the content in a string Jul 24, 2023 pm 10:28 PM

In Java, use the replace() method of the StringBuilder class to replace part of the content in a string. In Java programming, strings are a very important data type, and strings often need to be processed and manipulated. And sometimes we need to replace part of the string to meet our needs. In Java, you can use the replace() method of the StringBuilder class to implement string replacement operations. StringBuilder is a

How to use Pattern function in Java for pattern matching How to use Pattern function in Java for pattern matching Jun 26, 2023 pm 02:55 PM

In Java, the Pattern function is a very powerful and flexible tool that helps developers perform precise pattern matching in text. In this article, we will introduce how to use the Pattern function for pattern matching. The Pattern function is part of the Java.util.regex package, which allows developers to define and parse regular expressions. Regular expressions are a powerful tool for matching and manipulating text. It can be used to check the number and order of occurrences of letters, numbers, special characters, etc.

不用数据库来实现用户的简单的下载,代码如下,但是却不能下载,请高手找下原因,文件路劲什么的没有关问题 不用数据库来实现用户的简单的下载,代码如下,但是却不能下载,请高手找下原因,文件路劲什么的没有关问题 Jun 13, 2016 am 10:15 AM

不用数据库来实现用户的简单的下载,代码如下,但是却不能下载,请高手找下原因,文件路劲什么的没问题。

See all articles