Home >php教程 >PHP源码 >Summary of commonly used regular expressions in PHP

Summary of commonly used regular expressions in PHP

大家讲道理
大家讲道理Original
2016-11-08 13:41:20968browse

1. Regular expressions are often used when building websites. Here are some explanations and examples for your reference and modification only:
2. "^d+$" //Non-negative integer (positive integer + 0)
3. " ^[0-9]*[1-9][0-9]*$" //Positive integer
4. "^((-d+)|(0+))$" //Non-positive integer (negative integer + 0)
5. "^-[0-9]*[1-9][0-9]*$" // Negative integer
6. "^-?d+$" // Integer
7. "^ d+(.d+)?$" //Non-negative floating point number (positive floating point number + 0)
8. "^(([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
9. "^((-d+(.d+)?)|(0+(.0+)?))$" //Non-positive floating point number (negative floating point number + 0)
10. “^(-(([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
11. "^(-?d+) (.d+)?$" //Floating point number
12. "^[A-Za-z]+$" //A string consisting of 26 English letters
13. "^[A-Z]+$" // A string consisting of 26 uppercase English letters
14. "^[a-z]+$" //A string consisting of 26 lowercase English letters
15. "^[A-Za-z0-9]+ $" //A string consisting of numbers and 26 English letters
16. "^w+$" //A string consisting of numbers, 26 English letters or underscores
17. "^[w-]+(. [w-]+)*@[w-]+(.[w-]+)+$" //email address
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]))$/ // year -Month-Day
20. /^((0([1-9]{1}))|(1[1|2]))/(([0-2]([1-9]{1} ))|(3[0|1]))/(d{2}|d{4})$/ // Month/day/year
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]+) ?$/ //Phone number
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 address
24.
25. Regular expression matching Chinese characters: [u4e00-u9fa5]
26. Matching double-byte characters (including Chinese characters): [^x00-xff]
27. Matching blank lines Regular expression: n[s| ]*r
28. Regular expression matching HTML tags: /.*1>|/
29. Regular expression matching leading and trailing spaces: (^s*)|(s *$)
30. Regular expression to match email address: w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*
31. Match URL Regular expression: ^[a-zA-z]+://(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\S*)? $
32. Is the matching account legal (starting with a letter, 5-16 bytes allowed, alphanumeric underscores allowed): ^[a-zA-Z][a-zA-Z0-9_]{4,15}$
33 . Matching domestic phone numbers: (d{3}-|d{4}-)?(d{8}|d{7})?
34. Matching Tencent QQ numbers: ^[1-9]*[1- 9][0-9]*$
35.
36.
37. Metacharacters and their behavior in the context of regular expressions:
38.
​​39. Mark the next character as a special character, or a primitive escape character, or a backreference, or an octal escape character.
40.
41. ^ Matches the beginning of the input string. If the Multiline property of the RegExp object is set, ^ also matches the position after 'n' or 'r'.
42.
43. $ Matches the end position of the input string. If the Multiline property of the RegExp object is set, $ also matches the position before 'n' or 'r'.
44.
45. * Matches the previous subexpression zero or more times.
46.
47. + Matches the previous subexpression one or more times. + is equivalent to {1,}.
48.
49. ? Match the previous subexpression zero or one time. ? Equivalent to {0,1}.
50.
51. {n} n is a non-negative integer that matches a certain number of n times.
52.
53. {n,} n is a non-negative integer that matches at least n times.
54. ​
55. {n,m} m and n are both non-negative integers, where n 56.
57. ? When the character is immediately followed by any other limiter (*, +, ?, {n}, {n, }, {n,m}), the matching pattern is non-greedy. Non-greedy mode matches as little of the searched string as possible, while the default greedy mode matches as much of the searched string as possible.
58.
59. . Matches any single character except "n". To match any character including 'n', use a pattern like '[.n]'.
60. (pattern) matches pattern and gets this match.
61.
62. (?:pattern) matches pattern but does not get the matching result, which means that this is a non-getting match and is not stored for later use.
63.
64. (?=pattern) Forward lookup, match the search string at the beginning of any string matching pattern. This is a non-fetch match, that is, the match does not need to be fetched for later use.
65.
66. (?!pattern) negative lookup, opposite to (?=pattern)
67.
68. x|y matches x or y.
69.
70. [xyz] character set.
71.
72. [^xyz] Negative character set.
73.
74. [a-z] character range, matches any character within the specified range.
75.
76. [^a-z] Negative character range, matches any character that is not within the specified range.
77.
78. b matches a word boundary, which refers to the position between the word and the space.
79.
80. B matches non-word boundaries.
81.
82. cx matches the control character specified by x.
83.
84. d matches a numeric character. Equivalent to [0-9].
85.
86. D matches a non-numeric character. Equivalent to [^0-9].
87.
88. f matches a form feed. Equivalent to x0c and cL.
89.
90. n matches a newline character. Equivalent to x0a and cJ.
91.
92. r matches a carriage return character. Equivalent to x0d and cM.
93.
94. s matches any whitespace character, including spaces, tabs, form feeds, etc. Equivalent to [fnrtv].
95.
96. S matches any non-whitespace character. Equivalent to [^ fnrtv].
97.
98. t matches a tab character. Equivalent to x09 and cI.
99.
100. v matches a vertical tab character. Equivalent to x0b and cK.
101.
102. w matches any word character including an underscore. Equivalent to '[A-Za-z0-9_]'.
103.
104. W matches any non-word character. Equivalent to '[^A-Za-z0-9_]'.
105.
106. xn matches n, where n is the hexadecimal escape value. The hexadecimal escape value must be exactly two digits long.
107.
108. num matches num, where num is a positive integer. A reference to the match obtained.
109.
110. n identifies an octal escape value or a backreference. n is a backreference if n is preceded by at least n fetched subexpressions. Otherwise, if n is an octal number (0-7), then n is an octal escape value.
111.
112. nm identifies an octal escape value or a backreference. if nm is preceded by at least nm If a subexpression is obtained, nm is a back reference. If nm is preceded by at least n gets, then n is a backreference followed by the literal m. If none of the previous conditions are met, if n and m are both octal digits (0-7), then nm will match the octal escape value nm.
113.
114. nml If n is an octal digit (0-3), and m and l are both octal digits (0-7), then matches the octal escape value nml.
115.
116. un matches n, where n is a Unicode character represented by four hexadecimal digits.
117.
118. Regular expression matching Chinese characters: [u4e00-u9fa5]
119.
120. Matching double-byte characters (including Chinese characters): [^x00-xff]
121.
122. match Regular expression for empty lines: n[s| ]*r
123.
124. Regular expression for matching HTML tags: /.*1>|/
125.
126. Regular expression for matching leading and trailing spaces: (^s*)|(s*$)
127.
128. Regular expression matching email addresses: w+([-+.]w+)*@w+([-.]w+)*.w+([- .]w+)*
129.
130. Regular expression matching URL: http://([w-]+.)+[w-]+(/[w- ./?%&=]* )?
131.
132. Use regular expressions to limit the input content of text boxes in web forms:
133.
134. Use The regular expression limit can only be entered in Chinese: onkeyup="value=value.replace(/[^u4E00-u9FA5]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^u4E00-u9FA5]/g,''))"
135.
136. Use regular expressions to limit only Enter full-width characters: onkeyup="value=value.replace(/[^uFF00-uFFFF]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^uFF00-uFFFF]/g,''))"
137.
138. Use regular expressions to limit only Number of inputs Word: onkeyup="value=value.replace(/[^d]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^d]/g,''))"
139.
140. Use regular expressions to limit input numbers and english Text: onkeyup="value=value.replace(/[W]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^d]/g,''))"
141.      
142.   ======== =Commonly used regular expressions
143.
144.
145.
146. Regular expressions that match Chinese characters: [u4e00-u9fa5]
147.
148. Match double-byte characters (including Chinese characters): [^x00 -xff]
149.
150. Regular expression to match blank lines: n[s| ]*r
151.
152. Regular expression to match HTML tags: /.*1>|/
153.
154. Regular expression matching leading and trailing spaces: (^s*)|(s*$)
155.
156. Regular expression matching IP address: /(d+).(d+).(d+).(d+ )/g //
157.
158. Regular expression matching email addresses: w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*
159.
160. Regular expression matching URL: http://(/[w-]+.)+[w-]+(/[w- ./?%&=]*)? 161.
162. sql statement: ^(select|drop|delete|create|update|insert).*$
163.
164. 1. Non-negative integer: ^d+$
165.
166. 2. Positive integer: ^ [0-9]*[1-9][0-9]*$
167.
168. 3. Non-positive integer: ^((-d+)|(0+))$
169.
170. 4 , Negative integer: ^-[0-9]*[1-9][0-9]*$
171.
172. 5. Integer: ^-?d+$
173.
174. 6. Non-negative float Points: ^d+(.d+)?$
175.
176. 7. 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]*))$
177 .
178. 8. Non-positive floating point numbers: ^((-d+.d+)?)|(0+(.0+)?))$
179.
180. 9. Negative floating point numbers: ^(-( (Positive floating point regular expression)))$
181.
182. 10. English string: ^[A-Za-z]+$
183.
184. 11. English uppercase string: ^[A-Z]+$
185.
186. 12. English lowercase string: ^[a-z]+$
187.
188. 13. English character and numeric string: ^[A-Za-z0-9]+$
189.
190. 14. Alphanumeric and underlined string: ^w+$
191.
192. 15. E -Mail address: ^[w-]+(.[w-]+)*@[w-]+(.[w-]+)+$
193.
194. 16. URL: ^[a-zA -Z]+://(w+(-w+)*)(.(w+(-w+)*))*(?s*)?$
195. Or: ^http://[A-Za-z0 -9]+.[A-Za-z0-9]+[/=?%-&_~`@[]':+!]*([^""])*$
196. ​
197. 17 , Postal code: ^[1-9]d{5}$
198.
199. 18. Chinese: ^[u0391-uFFE5]+$
200.
201. 19. Phone number: ^(((d{ 2,3}))|(d{3}-))?((0d{2,3})|0d{2,3}-)?[1-9]d{6,7}(-d{ 1,4})?$
202.
203. 20. Mobile phone number: ^(((d{2,3}))|(d{3}-))?13d{9}$
204.
205 . 21. Double-byte characters (including Chinese characters): ^x00-xff
206.
207. 22. Match leading and trailing spaces: (^s*)|(s*$) (trim function like vbscript)
208.
209. 23. Match HTML tags: .*1>|
210.
211. 24. Match empty lines: n[s| ]*r
212.
213. 25. Extract network links in information (h|H)(r|R)(e|E)(f|F) *= *('|")?(w|\|/|.)+('|"| *|>)?
214.
215. 26. Extract the email address in the information: w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*
216.
217 . 27. Extract the picture link in the information: (s|S)(r|R)(c|C) *= *('|")?(w|\|/|.)+('|"| * |>)?
218.
219. 28. Extract the IP address in the information: (d+).(d+).(d+).(d+)
220.
221. 29. Extract the Chinese mobile phone number in the information: (86)*0*13d{9}
222.
223. 30. Extract the Chinese landline phone number in the information: ((d{3,4})|d{3,4}-|s)?d{ 8}
224.
225. 31. Extract the Chinese phone number in the information (including mobile and landline): ((d{3,4})|d{3,4}-|s)?d{7, 14}
226.
227. 32. Extract the Chinese postal code in the information: [1-9]{1}(d+){5}
228.
229. 33. Extract the floating point number (i.e. decimal) in the information (-?d*).?d+
230.
231. 34. Extract any number in the information: (-?d*)(.d+)? 232.
233. 35. IP: (d+). (d+).(d+).(d+)
234.
235. 36. Telephone area code: /^0d{2,3}$/
236.
237. 37. Tencent QQ number: ^[1-9] *[1-9][0-9]*$
238.
​​239. 38. Account number (starting with a letter, 5-16 bytes allowed, alphanumeric underscores allowed): ^[a-zA-Z][a- zA-Z0-9_]{4,15}$
240.
241. 39. Chinese, English, numbers and underline: ^[u4e00-u9fa5_a-zA-Z0-9]+$

Statement:
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