Home Backend Development PHP Tutorial Summary of commonly used regular expressions in PHP_PHP tutorial

Summary of commonly used regular expressions in PHP_PHP tutorial

Jul 14, 2016 am 10:10 AM
php under example Do and Commonly used yes regular Summary website expression Want to use explain

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 to match Chinese characters: [u4e00-u9fa5]
26. Match double-byte characters (including Chinese characters): [^x00-xff]
27. Regular expression to match blank lines: n[s| ]*r
28. Regular expression to match HTML tags: /<(.*)>.*|<(.*) />/
29. Regular expression matching leading and trailing spaces: (^s*)|(s*$)
30. Regular expression to match email address: w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*
31. Regular expression to match URL: ^[a-zA-z]+://(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\? \S*)?$
32. Whether the matching account is legal (starting with a letter, 5-16 bytes allowed, alphanumeric underscores allowed): ^[a-zA-Z][a-zA-Z0-9_]{4,15}$
33. Match domestic phone numbers: (d{3}-|d{4}-)?(d{8}|d{7})?
34. Matching Tencent QQ number: ^[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, a literal character, 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 <= m. Match at least n times and at most m times. There cannot be a space between the comma and the two numbers.
56. 
57. ? When the character immediately follows 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 obtain the matching result, which means that this is a non-acquisition match and is not stored for later use.
63. 
64. (?=pattern) Forward lookup, matches the search string at the beginning of any string that matches pattern. This is a non-fetch match, that is, the match does not need to be fetched for later use.
65. 
66. (?!pattern) negative preview, opposite to (?=pattern)
67. 
68. x|y matches x or y.
69. 
70. [xyz] character set.
71. 
72. [^xyz] Negative value 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 a word and a 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 character. 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 fetched subexpressions, nm is a backreference. If nm is preceded by at least n gets, then n is a backreference followed by the literal m. If neither of the previous conditions is true, and if n and m are both octal digits (0-7), 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 to match Chinese characters: [u4e00-u9fa5]
119. 
120. Match double-byte characters (including Chinese characters): [^x00-xff]
121. 
122. Regular expression to match blank lines: n[s| ]*r
123. 
124. Regular expression to match HTML tags: /<(.*)>.*|<(.*) />/
125. 
126. Regular expression matching leading and trailing spaces: (^s*)|(s*$)
127. 
128. Regular expression to match email addresses: w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*
129. 
130. Regular expression to match URL: http://([w-]+.)+[w-]+(/[w- ./?%&=]*)?
131. 
132. Use regular expressions to limit the input content of text boxes in web forms:
133. 
134. Use regular expressions to limit input to Chinese only: 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 the input of only 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 input to numbers: onkeyup="value=value.replace(/[^d]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text ').replace(/[^d]/g,''))"
139. 
140. Use regular expressions to limit input to numbers and English only: 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 expression to 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: /<(.*)>.*|<(.*) />/
153. 
154. Regular expression matching leading and trailing spaces: (^s*)|(s*$)
155. 
156. Regular expression to match IP address: /(d+).(d+).(d+).(d+)/g //
157. 
158. Regular expression to match email address: w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*
159. 
160. Regular expression to match 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 integers: ^((-d+)|(0+))$
169. 
170. 4. Negative integers: ^-[0-9]*[1-9][0-9]*$
171. 
172. 5. Integer: ^-?d+$
173. 
174. 6. Non-negative floating point number: ^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 number: ^(-((positive floating point number regular expression)))$
181. 
182. 10. English string: ^[A-Za-z]+$
183. 
184. 11. English capital string: ^[A-Z]+$
185. 
186. 12. English lowercase string: ^[a-z]+$
187. 
188. 13. English character and number string: ^[A-Za-z0-9]+$
189. 
190. 14. Alphanumeric plus underline 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: <(.*)>.*|<(.*) />
210. 
211. 24. Match blank lines: n[s| ]*r
212. 
213. 25. Extract network links in the 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 fixed phone number in the information: ((d{3,4})|d{3,4}-|s)?d{8}
224. 
225. 31. Extract Chinese phone numbers (including mobile and landline phones) from the information: ((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 floating point numbers (i.e. decimals) 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, allowing 5-16 bytes, allowing alphanumeric underscores): ^[a-zA-Z][a-zA-Z0-9_]{4,15}$
240. 
241. 39. Chinese, English, numbers and underline: ^[u4e00-u9fa5_a-zA-Z0-9]+$

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477510.htmlTechArticle1. Regular expressions are often used when making websites. Here are some explanations and examples for your reference only. Modified use: 2. ^d+$ //Non-negative integer (positive integer + 0) 3. ^[0-9]*[1-9][0-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)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles