Regular expressions commonly used in php (date telephone html Chinese email)

Source: Internet
Author: User
Tags comments html tags regular expression versions

This section describes some common regular expressions, such as regular expression verification of date, number, phone number, and time format.

Regular Expression Matching Chinese characters: [u4e00-u9fa5]
Note: matching Chinese characters is really a headache. It is easy to use this expression.
  
Match double byte characters (including Chinese characters): [^ x00-xff]
Comment: It can be used to calculate the length of a string (a dual-byte length meter 2, ASCII character meter 1)
  
Regular Expression Matching blank rows: ns * r
Comment: It can be used to delete blank rows.
  
Regular expressions matching HTML tags: <(S *?) [^>] *> .*? </1> & brvbar; <.*? />
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 * & brvbar; 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} & brvbar; 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} & brvbar; d {18}
Note: the ID card of China is 15 or 18 characters
  
Matched IP address: d +. d +
Comments: useful when extracting IP addresses
  
Match a specific number:
^ [1-9] d * $ // match a positive integer
^-[1-9] d * $ // match a negative integer
^ -? [1-9] d * $ // match the integer
^ [1-9] d * & brvbar; 0 $ // match a non-negative integer (positive integer + 0)
^-[1-9] d * & brvbar; 0 $ // Match non-positive integers (negative integers + 0)
^ [1-9] d *. d * & brvbar; 0. d * [1-9] d * $ // match the positive floating point number
^-([1-9] d *. d * & brvbar; 0. d * [1-9] d *) $ // match the negative floating point number
^ -? ([1-9] d *. d * & brvbar; 0. d * [1-9] d * & brvbar; 0 ?. 0 + & brvbar; 0) $ // Match floating point number
^ [1-9] d *. d * & brvbar; 0. d * [1-9] d * & brvbar; 0 ?. 0 + & brvbar; 0 $ // Match non-negative floating point number (positive floating point number + 0)
^ (-([1-9] d *. d * & brvbar; 0. d * [1-9] d *) & brvbar; 0 ?. 0 + & brvbar; 0 $ // Match 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:
^ [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

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.