Match a specific number:
| The code is as follows: |
Copy code |
^ [1-9] d * $ // match a positive integer ^-[1-9] d * $ // match a negative integer ^ -? [1-9] d * $ // match the integer ^ [1-9] d * | 0 $ // match a non-negative integer (positive integer + 0) ^-[1-9] d * | 0 $ // match a non-positive integer (negative integer + 0) ^ [1-9] d *. d * | 0. d * [1-9] d * $ // match the positive floating point number ^-([1-9] d *. d * | 0. d * [1-9] d *) $ // match the negative floating point number ^ -? ([1-9] d *. d * | 0. d * [1-9] d * | 0 ?. 0 + | 0) $ // Match floating point number ^ [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 a non-positive floating point number (negative floating point number + 0) |
Comments: It is useful when processing large amounts of data. Pay attention to correction when handling specific applications.
Match a specific string:
| The code is as follows: |
Copy code |
^ [A-Za-z] + $ // match A string consisting of 26 English letters ^ [A-Z] + $ // match a string consisting of 26 uppercase letters ^ [A-z] + $ // match a string consisting of 26 lowercase letters ^ [A-Za-z0-9] + $ // match a string consisting of digits and 26 letters ^ W + $ // match a string consisting of digits, 26 English letters, or underscores Comment: The most basic and commonly used expressions |
Regular Expression Matching Chinese characters: [u4e00-u9fa5]
Example
| The code is as follows: |
Copy code |
$ Str = "php programming "; If (preg_match ("/^ [x {4e00}-x {9fa5}] + $/u", $ str )){ Print ("all strings are Chinese "); } Else { Print ("Not all strings are Chinese "); } |
Note: matching Chinese characters is really a headache. It is easy to use this expression.
Match double byte characters (including Chinese characters): [^ x00-xff]
Example
| The code is as follows: |
Copy code |
$ Str = "singlepoint single point log "; If (preg_match ("/^ [x {4e00}-x {9fa5}] + $/u", $ str )){ Print ("all strings are Chinese "); } Else { Print ("Not all strings are Chinese "); } $ Alias_len = mb_strlen ($ value ['Alias'], "UTF-8 "); $ Temp_array = array (); For ($ I = 0; $ I <$ alias_len; $ I ++) { $ Temp_array [$ I] = mb_substr ($ value ['Alias'], $ I, 1, "UTF-8 "); If (ord (substr ($ temp_array [$ I], 0, 1)> '0xe0' & strlen ($ temp_array [$ I]) <3) $ Temp_array [$ I] = '';
} $ Value ['Alias'] = implode ('', $ temp_array ); |
Comments: encoding table dubyte character encoding range 1. GBK (GB2312/GB18030) x00-xff GBK dubyte encoding range x20-x7f ASCII
Xa1-xff Chinese gb2312 x80-xff Chinese gbk 2. UTF-8 (Unicode) u4e00-u9fa5 (Chinese) x3130-x318F (Korean
XAC00-xD7A3 (Korean) u0800-u4e00 (Japanese)
Regular Expression Matching blank rows: ns * r
| The code is as follows: |
Copy code |
$ Str = "123 456 "; $ Patten = "/s + /"; $ Result = split ($ patten, $ str ); Echo join ("<br>", $ result ); |
Comment: It can be used to delete blank rows.
Regular expressions matching HTML tags: <(S *?) [^>] *> .*? </1> | <.*? />
Comments: The versions circulating on the Internet are too bad. The above can only match some of them, and there is still nothing to do with complicated nested tags.
Regular expression matching the first and last blank characters: ^ s * | s * $
Comments: It can be used to delete spaces (including spaces, tabs, page breaks, and so on) at the beginning and end of a line. It is a very useful expression.
Regular Expression Matching the Email address: w + ([-+.] w +) * @ w + ([-.] w + )*. w + ([-.] w + )*
Comment: form verification is very useful
Regular Expression Matching URL: [a-zA-z] +: // [^ s] *
Comments: versions circulating on the Internet have limited functions. The above can basically meet the requirements.
Match account validity (starting with a letter, may be 5-16 bytes, may be an alphanumeric underline): ^ [a-zA-Z] [a-zA-Z0-9 _] {} $
Comment: form verification is very useful
Match Chinese phone number: d {3}-d {8} | d {4}-d {7}
Commentary: matching forms such as 0511-4405222 or 021-87888822
Match Tencent QQ number: [1-9] [0-9] {4 ,}
Comment: Tencent QQ number starts from 10000
Match China Zip code: [1-9] d {5 }(?! D)
Note: China post code is a six-digit number.
Matched ID card: d {15} | d {18}
Note: the ID card of China is 15 or 18 characters
Matched IP address: d +. d +
Comments: useful when extracting IP addresses
Something that may be useful to you related to regular expressions
One, a-z A-Z_0-9 // The most common character
Second, (bfw) (sda) // The unit symbol enclosed in parentheses. A bracket represents a whole.
3. [sdwe] [^ mjnb] // An atomic table enclosed in square brackets. The atomic table ^ indicates exclusion or the opposite content.
4. Escape characters
D contains all numbers [0-9]
D except all numbers [^ 0-9]
W contains all English characters [a-zA-Z_0-9]
W except all English characters [^ a-zA-Z_0-9] ----- match special characters
S contains blank areas such as carriage return, line feed, and paging [fnr]
4. Regular expression metacharacters
* Match the previous content 0 or multiple times
. Match content 0 or multiple times, but does not contain carriage return line breaks
+ Match the previous content once or multiple times
? Matches the previous content 0 times or 1 time
| Select matching, similar to php | usage
^ Match the content of the string header
$ Match string tail content
B matches the word boundary. The boundary can be a space or a special symbol.
B. Match the unexpected content except the word boundary.
{M} matches the previous content with m duplicates.
{M,} matches the repeat of the previous content more than or equal to m
{M, n} matches the number of repetitions of the previous content m to n
() Merge the overall match and put it into the memory. You can use \ 1 \ 2 to obtain the call in sequence.