Home > Article > Web Front-end > How to match numbers with regular expressions (with code)
This time I will bring you Regular expressionsHow to match numbers (with code), what are the precautions for regular expressions to match numbers, the following is a practical case, one Get up and take a look.
Regular expressions are used in string processing, form validation and other occasions, and are practical and efficient. Now we collect some commonly used expressions here for emergency use.
Regular expression for matching Chinese characters: [\u4e00-\u9fa5]
Comment: Matching Chinese characters is really a headache. With this expression, Easy to handle
Matches double-byte characters (including Chinese characters): [^\x00-\xff]
Comment: Can be used to calculate the length of a string (a double-byte Character length is counted as 2, ASCII characters are counted as 1)
Regular expression matching blank lines:\n\s*\r
Comment: Can be used to delete blank lines
Regular expression matching HTML tags: <(\S*?)[^>]*>.*?\1>|<.*? />
Comment : The version circulating on the Internet is too bad. The above one can only match part of it, and it is still powerless for complex nested tags.
Regular expression for matching leading and trailing whitespace characters: ^\s*|\s*$
Comment: It can be used to delete whitespace characters at the beginning and end of the line (including spaces, tabs, form feeds, etc.), a very useful expression
Regular expression that matches the email address :\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
Comment: It is very difficult to validate the form. Practical
Regular expression for matching URL: [a-zA-z]+://[^\s]*
Comment: The version circulating on the Internet has very limited functions. The above one Basically can meet the needs
Match whether the account is legal (starting with a letter, 5-16 bytes are allowed, alphanumeric underscores are allowed): ^[a-zA-Z][a-zA-Z0-9_]{4 ,15}$
Comment: Very practical during form verification
Matching domestic phone numbers:\d{3}-\d{8}|\d{4}-\d{7 }
Comment: The matching format is such as 0511-4405222 or 021-87888822
Matching Tencent QQ number: [1-9][0-9]{4,}
Comment: Tencent QQ number starts from 10000
Matches Chinese postal code: [1-9]\d{5}(?!\d)
Comment: Chinese postal code is 6 digits
Matching ID card:\d{15}|\d{18}
Comment: China’s ID card is 15 or 18 digits
Matching ip address:\ d+\.\d+\.\d+\.\d+
Comment: Useful when extracting IP address
match specific numbers:
^[ 1-9]\d*$ // Match positive integers
^-[1-9]\d*$ // Match negative integers
^-?[1-9]\d*$ // Match Integer
^[1-9]\d*|0$ // Matches non-negative integers (positive integers + 0)
^-[1-9]\d*|0$ // Matches non-positive integers (Negative integer + 0)
^[1-9]\d*\.\d*|0\.\d*[1-9]\d*$ //Match positive floating point numbers
^- ([1-9]\d*\.\d*|0\.\d*[1-9]\d*)$ //Match negative floating point numbers
^-?([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 numbers (positive floating point numbers + 0)
^(- ([1-9]\d*\.\d*|0\.\d*[1-9]\d*))|0?\.0+|0$ //Match non-positive floating point numbers (negative Floating point number + 0)
Comment: Useful when processing large amounts of data, pay attention to corrections during specific applications
Match specific strings:
^[A -Za-z]+$ // Matches a string consisting of 26 English letters
^[A-Z]+$ // Matches a string consisting of 26 uppercase English letters
^[a-z]+ $ // Matches a string consisting of 26 lowercase English letters
^[A-Za-z0-9]+$ // Matches a string consisting of numbers and 26 English letters
^\w+ $ //Match a string consisting of numbers, 26 English letters or underscores
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website!
Recommended reading:
Detailed graphic explanation of using regular multi-line mode and single-line mode
Regular non-capturing group Detailed explanation of using capture groups
The above is the detailed content of How to match numbers with regular expressions (with code). For more information, please follow other related articles on the PHP Chinese website!