Common PHP Regular expressions and grammatical annotations

Source: Internet
Author: User

Common PHP Regular expressions and grammatical annotations:





a regular expression that matches a Chinese character: [U4e00-u9fa5]


annotation: Match Chinese is really a headache, with this expression is good to do





match Double-byte characters (including Chinese characters): [^x00-xff]


Note: Can be used to compute the length of a string (a two-byte character-length meter 2,ascii character count 1)





a regular expression that matches a blank row: Ns*r


Note: Can be used to delete blank rows





Regular expression:< matching HTML tags (s*?) [^>]*>.*?| <.*? />


Note: The online version is too bad, the above can only match the part of the complex nested tags still powerless





a regular expression that matches the end-end whitespace character: ^s*|s*$


Note: A useful expression that can be used to delete white space characters (including spaces, tabs, page breaks, and so on) that are at the end of a line beginning





a regular expression matching an email address: w+ ([-+.] w+) *@w+ ([-.] w+) *.w+ ([-.] w+) *


Note: Form validation is practical





a regular expression that matches URL URLs: [a-za-z]+://[^s]*


Note: Online circulation of the version of the function is very limited, above this basic can meet the demand





Match account number is legal (beginning of letter, allow 5-16 bytes, allow alphanumeric underline): ^[a-za-z][a-za-z0-9_]{4,15}$


Note: Form validation is practical





matching domestic phone number: D{3}-d{8}|d{4}-d{7}


annotation: Match form such as 0511-4405222 or 021-87888822





matching Tencent QQ number: [1-9][0-9]{4,}


annotation: Tencent QQ number starts from 10000





matching China zip code: [1-9]d{5} (?! d)


Note: China's postal code is 6 digits





matching ID: d{15}|d{18}


NOTE: China's ID card is 15-or 18-bit





Matching IP address: d+.d+.d+.d+


Note: Useful when extracting IP addresses





matches a specific number:


^[1-9]d*$//matching positive integer


^-[1-9]d*$//matching negative integer


^-? [1-9]d*$//matching integer


^[1-9]d*|0$//matching nonnegative integer (positive integer + 0)


^-[1-9]d*|0$//matching non positive integer (negative integer + 0)


^[1-9]d*.d*|0.d*[1-9]d*$//matching positive floating-point number


^-([1-9]d*.d*|0.d*[1-9]d*) $///matching negative floating-point number


    ^-? ([1-9]d*.d*|0.d*[1-9]d*|0?. 0+|0) $///matching floating-point number


^[1-9]d*.d*|0.d*[1-9]d*|0? 0+|0$//matching nonnegative floating-point number (positive floating-point number + 0)


^ (-([1-9]d*.d*|0.d*[1-9]d*)) | 0+|0$//matching non-positive floating-point numbers (negative floating-point number + 0)


annotation: Useful when dealing with a large amount of data, pay attention to correction when applying





match a specific string:


^[a-za-z]+$//Match a string of 26 English letters


^[a-z]+$//Match a string consisting of 26 uppercase letters


^[a-z]+$//matches a string consisting of 26 lowercase letters


^[a-za-z0-9]+$//Match a string of numbers and 26 English letters


^w+$//matches a string of numbers, 26 English letters, or underscores


annotation: The most basic and most commonly used expressions


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.