PHP Common Regular Expression Collection _php example

Source: Internet
Author: User
Tags control characters

Usually do the site often to use regular expressions, the following are some explanations and examples, only for everyone to refer to and modify the use of:  

  "^\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 number (positive 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]*)] $ "//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]+$ "//////26-Letter string" ^[a-z]+$ "///26-Letter uppercase string" ^[a-z]+ $ "///////26 alphanumeric string" ^[a-za-z0-9]+$ "////" ^\w+$ "////by number and 26-letter string" ^[\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


  A regular expression that matches a Chinese character: [\u4e00-\u9fa5] 
  matches double-byte characters (including Chinese characters): [^\x00-\xff] 
  a regular expression that matches a blank row: \n[\s|] *\r regular expression that 
  matches the HTML tag:/< (. *) >.*<\/\1>|< (. *) \/>/ 
  matches a regular expression with a trailing space: (^\s*) | ( \s*$) 
  A regular expression matching an email address: \w+ ([-+.] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] \w+) * 
  a regular expression matching URL URLs: ^[a-za-z]+://(\\w+ (-\\w+) *) (\ \\w+ (-\\w+) *)) * (\\?\\s*)? $ 
  Match account number is valid (start letter, allow 5-16 bytes, allow alphanumeric underline): ^[a-za-z][a-za-z0-9_]{4,15}$ 
  Matching domestic phone number: (\d {3}-|\d{4}-)? (\d{8}|\d{7})? 
  Matching Tencent QQ Number: ^[1-9]*[1-9][0-9]*$ 


Metacharacters and its behavior in the context of regular expressions:

  
  \ marks the next character as a special character, or a literal character, or a back reference, or a octal escape character. ^ matches the start position of the input string. 
  If the multiline property of the RegExp object is set, ^ also matches the position after ' \ n ' or ' \ R '. $ matches the end position of the input string. 
  If the multiline property of the RegExp object is set, the $ also matches the position before ' \ n ' or ' \ R '. 
  * Match the preceding subexpression 0 or more times. + matches the preceding subexpression one or more times. + equivalent to {1,}.? Match the preceding subexpression 0 times or once.? 
  is equivalent to {0,1}. 
  {n} n is a non-negative integer that matches the determined N times. 
  {N,} n is a non-negative integer that matches at least n times. {n,m} m and n are non-negative integers, where n <= m. Matches n times at least and matches up to M times. There should be no space between the comma and the two number. When the character is immediately following any of the other qualifiers (*, +,?, {n}, {n,}, {n,m}), the matching pattern is not greedy. Non-greedy patterns match as few strings as possible, while the default greedy pattern matches as many of the searched strings as possible. Matches any single character except "\ n". 
  To match any character including ' \ n ', use a pattern like ' [. \ n] '. 
  (pattern) matches the pattern and gets the match. 
  (?:p Attern) matches pattern but does not get matching results, which means that this is a non fetch match and is not stored for later use. (? =pattern) forward lookup, matching the find string at the beginning of any string matching pattern. 
  This is a non-fetch match, that is, the match does not need to be acquired for later use. (?! 
  pattern) Negative forward check, and (? =pattern) function opposite x|y match x or Y. 
  [XYZ] Character set combination. 
  [^XYZ] Negative character set combination. 
  [A-z] character range that matches any character within the specified range. 
  [^a-z] A negative character range that matches any character that is not in the specified range. 
  \b Matches a word boundary, which refers to the position between the word and the space. 
  \b Matches a non word boundary. 
  \CX matches the control characters indicated by X. \d matches a numeric character. 
  equivalent to [0-9]. \d matches a non-numeric character。 
  equivalent to [^0-9]. \f matches a page feed character. 
  Equivalent to \x0c and \CL. \ n matches a newline character. 
  Equivalent to \x0a and \CJ. \ r matches a carriage return character. 
  Equivalent to \x0d and \cm. \s matches any white space character, including spaces, tabs, page breaks, and so on. 
  equivalent to [\f\n\r\t\v]. \s matches any non-white-space character. 
  equivalent to [^ \f\n\r\t\v]. \ t matches a tab character. 
  Equivalent to \x09 and \ci. \v matches a vertical tab. 
  Equivalent to \x0b and \ck. \w matches any word character that includes an underscore. 
  Equivalent to ' [a-za-z0-9_] '. \w matches any non word character. 
  Equivalent to ' [^a-za-z0-9_] '. \XN matches N, where n is the hexadecimal escape value. 
  The hexadecimal escape value must be a determined two digits long. \num matches num, where num is a positive integer. 
  A reference to the match that was obtained. \ n identifies a octal escape value or a back reference. N is a back reference if you have at least N obtained subexpression before. 
  Otherwise, if n is an octal number (0-7), then N is an octal escape value. \NM identifies a octal escape value or a back reference. NM is a \nm if at least one of the preceded by the at least NM gets the subexpression before the If there are at least N fetches before \nm, then N is a back reference followed by a literal m. 
  If all the preceding conditions are not satisfied, if both N and M are octal digits (0-7), then \nm will match octal escape value nm. 
  \NML if n is an octal number (0-3) and both M and L are octal digits (0-7), then the octal escape value NML is matched. 
 \un matches N, where N is a Unicode character represented in four hexadecimal digits.

   

 A regular expression that matches a Chinese character: [U4e00-u9fa5] matches double-byte characters (including Chinese characters): [^x00-xff] A regular expression that matches a blank row: n[s|] *r regular expression that matches the HTML tag:/< (. *) >.*</1>|< (. *)/>/matches a regular expression with a trailing space: (^s*) | (s*$) A regular expression matching an email address: w+ ([-+.] w+) *@w+ ([-.] w+) *.w+ ([-.] w+) * Matching the URL of the regular expression: http://([w-]+.) 
  +[w-]+ (/[w-/?%&=]*)? Using regular expressions to restrict text box entries in a Web page form: Use regular expression restrictions to enter only Chinese: onkeyup= "value=value.replace (/[^u4e00-u9fa5]/g,") "onbeforepaste=" Clipboarddata.setdata (' Text ', Clipboarddata.getdata (' text '). Replace (/[^u4e00-u9fa5]/g, ') "restricts only full-width characters in regular expression: Onkeyup= "Value=value.replace (/[^uff00-uffff]/g,") "Onbeforepaste=" Clipboarddata.setdata (' text ', Clipboarddata.getdata (' text '). Replace (/[^uff00-uffff]/g, ') "restricts only numbers to be entered with regular expressions: onkeyup= value=value.replace (/[^D) 
  /g, "onbeforepaste=" clipboarddata.setdata (' text ', Clipboarddata.getdata (' text '). Replace (/[^d]/g, ') " Only numbers and English can be entered with regular expression restrictions: onkeyup= "Value=value.replace (/[w]/g,") "Onbeforepaste=" Clipboarddata.setdata (' text ', 
 Clipboarddata.getdata (' text '). Replace (/[^d]/g, ') "


========= Common Regular formula

A regular expression that matches a Chinese character: [\u4e00-\u9fa5] 
  matches double-byte characters (including Chinese characters): [^\x00-\xff] 
  a regular expression that matches a blank row: \n[\s|] *\r regular expression that 
  matches the HTML tag:/< (. *) >.*<\/\1>|< (. *) \/>/ 
  matches a regular expression with a trailing space: (^\s*) | ( \s*$) 
  a regular expression that matches an IP address:/(\d+) \. ( \d+) \. (\d+) \. (\d+)/g// 
  matching an email address regular expression: \w+ ([-+.] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] \w+) * 
  matching the URL of the regular expression: 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+$ nonnegative floating-point number: ^\d+ (\.\d+)? $ positive float: ^ (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 number: ^ ((-\d+\.\d+)?) | (0+ (\.0+)) $ negative Floating point number: ^ (-(positive floating-point number regular)) $ English string: ^[a-za-z]+$ English uppercase string: ^[a-z]+$ English lowercase string: ^[a-z]+$ English character string: ^[a-za-z0-9]+$ English numerals underlined: ^\w+$ E-mai L 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 Number: ^ ((\d{2,3}\)) | ( \d{3}\-)? 13\d{9}$ double-byte characters (including Chinese characters): ^\x00-\xff matching the trailing spaces: (^\s*) | (\s*$) (Trim function like VBScript) matching HTML tags:< (. *) >.*<\/\1>|< (. *) \/> matching blank rows: \n[\s|] *\r the network link in the extraction information: (h| H) (r| R) (e| E) (f| F) *= * (' | ')? (\w|\\|\/|\.) +('|"| 
*|>)? Message address in the extraction information: \w+ ([-+.] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] \w+) * Extract the picture link in the information: (s| S) (R| R) (c| C) *= * (' | ')? (\w|\\|\/|\.) +('|"| 
*|>)? Extract the IP address in the information: (\d+) \. (\d+) \. (\d+) \. (\d+) Extract the information from the Chinese mobile Number: (*0*13\D{9) to extract information from the Chinese fixed telephone number: ((\d{3,4}\) |\d{3,4}-|\s)? \d{8 to extract the Chinese telephone number (including mobile and fixed telephone) in the information: (\d{ 3,4}\) |\d{3,4}-|\s? \d{7,14} Extract information from china ZIP code: [1-9]{1} (\d+) {5} Extract floating-point number (that is, decimal) in 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 Number: ^[1-9]*[1-9][0-9]*$ account number (beginning of letter, allow 5-16 bytes, allow alphanumeric underline): ^[a-za-z][a-za-z0-9_]{4,15}$ Chinese, English,
 Numbers and underscores: ^[\u4e00-\u9fa5_a-za-z0-9]+$

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.