Common regular expression encyclopedia (numbers, characters, fixed format) _ Regular expressions

Source: Internet
Author: User

Related reading:

Common regular expressions for iOS development

Java Regular Expressions filter out letters, numbers, and Chinese

A regular expression (regular expression) describes a pattern of string matching that can be used to check whether a string contains a seed string, replaces a matching substring, or extracts a substring from a string that matches a condition.

Column directory, the *.txt in dir *.txt or LS *.txt is not a regular expression, because the meaning of this * is different from that of the regular type.

The method for constructing regular expressions is the same as for creating mathematical expressions. That is, multiple metacharacters and operators can combine small expressions to create larger expressions. The component of a regular expression can be a single character, character set, character range, selection between characters, or any combination of any of these components.

A regular expression is a literal pattern composed of ordinary characters, such as characters A through z, and special characters, called "metacharacters". The pattern describes one or more strings to match when searching for text. A regular expression is used as a template to match a character pattern with the string being searched for.

The following is the daily collation of the small series of regular expressions commonly used in the work, commonly used regular validation expressions:

Number-related regular expressions

Number: ^[0-9]*$

N-bit fixed-length number: ^\d{n}$

Number between m-n bits: ^\d{m,n}$

Numbers beginning with 0 and not 0: ^ (0|[ 1-9][0-9]*) $

A number with a maximum of two decimal digits at the beginning of a non 0: ^ ([1-9][0-9]*) + (. [ 0-9]{1,2})? $

Positive or negative numbers with 1-2 decimal places: ^ (\-) \d+ (\.\d{1,2})? $

Positive, negative, and decimal: ^ (\-|\+) \d+ (\.\d+)? $

Positive real number with two decimal digits: ^[0-9]+ (. [ 0-9]{2})? $

Positive real numbers with 1~3 decimal places: ^[0-9]+ (. [ 0-9]{1,3})? $

Positive integer: ^\d+$ or ^[1-9]\d*|0$

Negative integer: ^-[1-9]\d*|0$ or ^ (-\d+) | ( 0+)) $

Positive floating-point number: ^[1-9]\d*\.\d*|0\.\d*[1-9]\d*$ or ^ ([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: ^-([1-9]\d*\.\d*|0\.\d*[1-9]\d*) $ or ^ (-([0-9]+\.[ 0-9]*[1-9][0-9]*) | ([0-9]*[1-9][0-9]*\. [0-9]+) | ([0-9]*[1-9][0-9]*))] $

Floating-point number: ^ (-?\d+) (\.\d+)? $ or ^-? ([1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0) $

Character-related regular expressions

Chinese characters: ^[\u4e00-\u9fa5]{0,}$

All characters of length 3-20: ^. {3,20}$

A string consisting of numbers and 26 English letters: ^[a-za-z0-9]+$

A string consisting of numbers, 26 English letters, or underscores: ^\w+$ or ^\w{3,20}$

Chinese, English, digital include underline: ^[\u4e00-\u9fa5a-za-z0-9_]+$

Symbols in Chinese, English, digital but excluding underscores: ^[\u4e00-\u9fa5a-za-z0-9]+$ or ^[\u4e00-\u9fa5a-za-z0-9]{2,20}$

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

Password (beginning with a letter, length between 6~18, only letters, numbers, and underscores): ^[a-za-z]\w{5,17}$

Strong passwords (must contain combinations of uppercase and lowercase letters and numbers, cannot use special characters, length between 8-10): ^ (? =.*\d) (? =.*[a-z]) (? =.*[a-z]). {8,10}$

Regular expressions in fixed format

Email Address: ^\w+ ([-+.] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] \w+) *$

Site domain name: [a-za-z0-9][-a-za-z0-9]{0,62} (/.[ A-ZA-Z0-9][-A-ZA-Z0-9]{0,62}) +/.?

URL URLs: ^http://([\w-]+\.) +[\w-]+ (/[\w-./?%&=]*)? $

Mobile Number: ^ (13[0-9]|17[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8| 9]|18[0|1|2|3|5|6|7|8| 9]) \d{8}$

Phone numbers ("Xxx-xxxxxxx", "xxxx-xxxxxxxx", "xxx-xxxxxxx", "xxx-xxxxxxxx", "XXXXXXX" and "XXXXXXXX"): ^ (\d{3,4}-) |\d{3.4}-)? \ d{7,8}$

Domestic telephone number (0511-4405222, 021-87888822): \d{3}-\d{8}|\d{4}-\d{7}

ID number (15 digits, 18 digits): ^\d{15}|\d{18}$

Date format: ^\d{4}-\d{1,2}-\d{1,2}

12 months of the year (01~09 and 1~12): ^ (0?[ 1-9]|1[0-2]) $

One months of 31 days (01~09 and 1~31): ^ (0?[ 1-9]) | ((1|2) [0-9]) |30|31) $

Tencent QQ number: [1-9][0-9]{4,8} (Tencent QQ number starts from 10000, currently has a maximum of 10 digits)

China postal Code: [1-9]\d{5} (?! \d) (China postal Code is 6 digits)

IP address: \d+\.\d+\.\d+\.\d+ (useful when extracting IP addresses)

IP address: (?:(? : 25[0-5]|2[0-4]\\d| [01]?\\d?\\d) \.) {3} (?: 25[0-5]|2[0-4]\\d| [01]?\\d?\\d)]

The above is a small set to introduce the commonly used regular expression encyclopedia (numbers, characters, fixed format), I hope to help you!

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.