Home  >  Article  >  php教程  >  The most comprehensive commonly used regular expressions

The most comprehensive commonly used regular expressions

大家讲道理
大家讲道理Original
2016-11-10 10:06:271588browse

1. Expression of check digits

1 Number: ^[0-9]*$

2 n-digit number: ^d{n}$

3 At least n-digit number: ^d{n, }$

4 m-n digit numbers: ^d{m,n}$

5 Numbers starting with zero and non-zero: ^(0|[1-9][0-9]*)$

6 Non Numbers starting with zero with up to two decimal places: ^([1-9][0-9]*)+(.[0-9]{1,2})?$

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

8 Positive numbers, negative numbers, and decimals: ^(-|+)?d+(.d+)?$

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

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

11 Non-zero positive integer: ^[1-9]d*$ or ^([1-9][0-9]*){1, 3}$ or ^+?[1-9][0-9]*$

12 Non-zero negative integer: ^-[1-9][]0-9"*$ or ^-[1-9 ]d*$

13 Non-negative integer: ^d+$ or ^[1-9]d*|0$

14 Non-positive integer: ^-[1-9]d*|0$ or ^((- d+)|(0+))$

15 Non-negative floating point number: ^d+(.d+)?$ or ^[1-9]d*.d*|0.d*[1-9]d*| 0?.0+|0$

16 Non-positive floating point number: ^((-d+(.d+)?)|(0+(.0+)?))$ or ^(-([1-9] d*.d*|0.d*[1-9]d*))|0?.0+|0$

17 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]*))$

18 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]*)) )$

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


2. Expression of check characters

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

2 English and numbers: ^[A-Za-z0 -9]+$ or ^[A-Za-z0-9]{4,40}$

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

4 consisting of 26 English letters String consisting of: ^[A-Za-z]+$

5 String consisting of 26 uppercase English letters: ^[A-Z]+$

6 String consisting of 26 lowercase English letters: ^ [a-z]+$

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

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

9 Chinese, English, numbers including underscores: ^[u4E00-u9FA5A-Za-z0-9_]+$

10 Chinese, English, numbers but not including underscores and other symbols :^[u4E00-u9FA5A-Za-z0-9]+$ or ^[u4E00-u9FA5A-Za-z0-9]{2,20}$

11 can be entered containing ^%&',;=?$" Characters such as: [^%&',;=?$x22]+

12 It is forbidden to enter characters containing ~: [^~x22]+


3. Expressions for special needs

1 Email address: ^ w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*$

2 Domain name: [a-zA-Z0-9][-a-zA -Z0-9]{0,62}(/.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+/.?

3 InternetURL: [a- zA-z]+://[^s]* or ^http://([w-]+.)+[w-]+(/[w-./?%&=]*)?$

4 Mobile phone number: ^(13[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}$

5 phone number ("XXX-XXXXXXX", "XXXX-XXXXXXXX", "XXX-XXXXXX", "XXX-XXXXXXXX", "XXXXXXX "and"XXXXXXXX): ^((d{3,4}-)|d{3.4}-)?d{7,8}$

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

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

8 Short ID number (Ending with numbers and letters x): ^([0-9]){7,18}(x|X)?$ or ^d{8,18}|[0-9x]{8,18}|[0 -9X]{8,18}?$

9 Is the account legal (starting with a letter, 5-16 bytes allowed, alphanumeric underscores allowed): ^[a-zA-Z][a-zA-Z0-9_] {4,15}$

10 Password (starting with a letter, length between 6~18, can only contain letters, numbers and underscores): ^[a-zA-Z]w{5,17}$

11 Strong password (must contain a combination of uppercase and lowercase letters and numbers, cannot use special characters, length is between 8-10): ^(?=.*d)(?=.*[a-z])(?=.* [A-Z]).{8,10}$

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

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

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

15 Input format of money:

16 1. There are four representations of money that we can accept: "10000.00" and " 10,000.00", and "10000" and "10,000" without "cent": ^[1-9][0-9]*$

17 2. This means any number that does not start with 0, but this also It means that a character "0" is not passed, so we use the following form: ^(0|[1-9][0-9]*)$

18 3. A 0 or a number that does not start with 0. We can also allow a negative sign at the beginning: ^(0|-?[1-9][0-9]*)$

19 4. This means a 0 or a number that does not start with 0 that may be negative Let the user start with 0. Also remove the negative sign, because money cannot be negative. What we need to add next is to indicate the possible decimal part: ^[0-9]+(.[0-9 ]+)?$

20 5. It must be noted that there should be at least 1 digit after the decimal point, so "10." is not passed, but "10" and "10.2" are passed: ^[0- 9]+(.[0-9]{2})?$

21 6. In this way, we stipulate that there must be two digits after the decimal point. If you think it is too harsh, you can do this: ^[0-9]+(. [0-9]{1,2})?$

22 7. This allows the user to write only one decimal place. Next we should consider the comma in the number, we can do this: ^[0-9]{1 ,3}(,[0-9]{3})*(.[0-9]{1,2})?$

23 8.1 to 3 numbers, followed by any number of commas + 3 numbers, commas become optional instead of required: ^([0-9]+|[0-9]{1,3}(,[0 -9]{3})*)(.[0-9]{1,2})?$

24 Note: This is the final result, don't forget that "+" can be replaced with "*" if you feel If the empty string is also acceptable (strange, why?) Finally, don’t forget to remove the backslash when using the function. Common errors are here

25 xml file: ^([a-zA-Z] +-?)+[a-zA-Z0-9]+\.[x|X][m|M][l|L]$

26 Regular expression for Chinese characters: [u4e00-u9fa5]

27 Double-byte characters: [^x00-xff] (including Chinese characters, can be used to calculate the length of a string (the length of a double-byte character counts as 2, and the length of an ASCII character counts as 1)) 28 Regular expression for blank lines: ns*r (can be used to delete blank lines)

29 Regular expression for HTML tags: ]*>.*?1>|<.> (The version circulating on the Internet is too bad. The above one is only partially effective and is still powerless for complex nested tags.) 30 Regular expression for leading and trailing whitespace characters: ^s*|s*$ or (^s*)|(s*$) (It can be used to delete blank characters at the beginning and end of the line (including spaces, tabs, form feeds, etc.), a very useful expression)

31 Tencent QQ number: [1-9][0-9] {4,} (Tencent QQ number starts from 10000)

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

33 IP address: d+. d+.d+.d+ (useful when extracting IP address) 34 IP address: ((?:(?:25[0-5]|2[0-4]\d|[01]?\d?\d)\ .){3}(?:25[0-5]|2[0-4]\d|[01]?\d?\d))

"^d+$" //Non-negative integer ( Positive integer + 0)
"^[0-9]*[1-9][0-9]*$" //Positive integer
"^((-d+)|(0+))$" //Not 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]+$" //A string consisting of 26 English letters
"^[A-Z]+$" //A string consisting of 26 uppercase English letters
" ^[a-z]+$" //A string consisting of 26 lowercase English letters
"^[A-Za-z0-9]+$" //A string consisting of numbers and 26 English letters
" ^w+$" //A string consisting of numbers, 26 English letters or underscores
"^[w-]+(.[w-]+)*@[w-]+(.[w-]+) +$" //email address
"^[a-zA-z]+://(w+(-w+)*)(.(w+(-w+)*))*(?S*)?$" / /url

Integer or decimal: ^[0-9]+.{0,1}[0-9]{0,2}$
Only numbers can be entered: "^[0-9]*$".
Only n-digit numbers can be entered: "^d{n}$".
You can only enter a number with at least n digits: "^d{n,}$".
Only m~n digits can be entered:. "^d{m,n}$"
You can only enter numbers starting with zero and non-zero: "^(0|[1-9][0-9]*)$".
Only positive real numbers with two decimal places can be entered: "^[0-9]+(.[0-9]{2})?$".
You can only enter positive real numbers with 1~3 decimal places: "^[0-9]+(.[0-9]{1,3})?$".
Only non-zero positive integers can be entered: "^+?[1-9][0-9]*$".
Only non-zero negative integers can be entered: "^-[1-9][]0-9"*$.
Only characters with a length of 3 can be entered: "^.{3}$".
You can only enter a string consisting of 26 English letters: "^[A-Za-z]+$".
You can only enter a string consisting of 26 uppercase English letters: "^[A-Z]+$".
You can only enter a string consisting of 26 lowercase English letters: "^[a-z]+$".
You can only enter a string consisting of numbers and 26 English letters: "^[A-Za-z0-9]+$".
You can only enter a string consisting of numbers, 26 English letters or underscores: "^w+$".
Verify user password: "^[a-zA-Z]w{5,17}$" The correct format is: starting with a letter, the length is between 6 and 18, and can only contain characters, numbers and underscores.
Verify whether it contains characters such as ^%&'',;=?$": "[^%&'',;=?$x22]+".
Only Chinese characters can be entered: "^[u4e00-u9fa5]{0 ,}$"
Verify email address: "^w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*$".
Verify InternetURL: "^ http://([w-]+.)+[w-]+(/[w-./?%&=]*)?$".
Verification phone number: "^((d{3,4}-)|d{3.4}-)?d{7,8}$" The correct format is: "XXX-XXXXXXX", "XXXX-XXXXXXXX", "XXX -XXXXXXX", "XXX-XXXXXXXX", "XXXXXXX" and "XXXXXXXX".
Verify ID number (15 or 18 digits): "^d{15}|d{18}$".
Verify the 12 months of a year: "^(0?[1-9]|1[0-2])$" The correct format is: "01"~"09" and "1"~"12".
Verify the 31 days of a month: "^((0?[1-9])|((1|2)[0-9])|30|31)$" The correct format is; "01"~"09 "and"1"~"31". Integer or decimal: ^[0-9]+.{0,1}[0-9]{0,2}$
Only numbers can be entered: "^[0-9]*$".
Only n-digit numbers can be entered: "^d{n}$".
You can only enter a number with at least n digits: "^d{n,}$".
Only m~n digits can be entered:. "^d{m,n}$"
You can only enter numbers starting with zero and non-zero: "^(0|[1-9][0-9]*)$".
Only positive real numbers with two decimal places can be entered: "^[0-9]+(.[0-9]{2})?$".
You can only enter positive real numbers with 1~3 decimal places: "^[0-9]+(.[0-9]{1,3})?$".
Only non-zero positive integers can be entered: "^+?[1-9][0-9]*$".
Only non-zero negative integers can be entered: "^-[1-9][]0-9"*$.
Only characters with a length of 3 can be entered: "^.{3}$".
You can only enter a string consisting of 26 English letters: "^[A-Za-z]+$".
You can only enter a string consisting of 26 uppercase English letters: "^[A-Z]+$".
You can only enter a string consisting of 26 lowercase English letters: "^[a-z]+$".
You can only enter a string consisting of numbers and 26 English letters: "^[A-Za-z0-9]+$".
You can only enter a string consisting of numbers, 26 English letters or underscores: "^w+$".
Verify user password: "^[a-zA-Z]w{5,17}$" The correct format is: starting with a letter, the length is between 6 and 18, and can only contain characters, numbers and underscores.
Verify whether it contains characters such as ^%&'',;=?$": "[^%&'',;=?$x22]+".
Only Chinese characters can be entered: "^[u4e00-u9fa5]{0 ,}$"
Verify email address: "^w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*$".
Verify InternetURL: "^ http://([w-]+.)+[w-]+(/[w-./?%&=]*)?$".
Verification phone number: "^((d{3,4}-)|d{3.4}-)?d{7,8}$" The correct format is: "XXX-XXXXXXX", "XXXX-XXXXXXXX", "XXX -XXXXXXX", "XXX-XXXXXXXX", "XXXXXXX" and "XXXXXXXX".
Verify ID number (15 or 18 digits): "^d{15}|d{18}$".
Verify the 12 months of a year: "^(0?[1-9]|1[0-2])$" The correct format is: "01"~"09" and "1"~"12".
Verify the 31 days of a month: "^((0?[1-9])|((1|2)[0-9])|30|31)$" The correct format is; "01"~"09 "and"1"~"31".
"^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]+$" //A string composed of 26 English letters
"^[A-Z]+$" //Consisted of A string of 26 uppercase English letters
"^[a-z]+$" //A string of 26 lowercase English letters
"^[A-Za-z0-9]+$" //A string consisting of numbers and 26 English letters
"^w+$" //A string consisting of numbers, 26 English letters or underscores
" ^[w-]+(.[w-]+)*@[w-]+(.[w-]+)+$" //email address
"^[a-zA-z]+:// (w+(-w+)*)(.(w+(-w+)*))*(?S*)?$" //url

Integer or decimal: ^[0-9]+.{0,1}[0-9]{0,2}$
Only numbers can be entered: "^[0-9]*$".
Only n-digit numbers can be entered: "^d{n}$".
You can only enter a number with at least n digits: "^d{n,}$".
Only m~n digits can be entered:. "^d{m,n}$"
You can only enter numbers starting with zero and non-zero: "^(0|[1-9][0-9]*)$".
Only positive real numbers with two decimal places can be entered: "^[0-9]+(.[0-9]{2})?$".
You can only enter positive real numbers with 1~3 decimal places: "^[0-9]+(.[0-9]{1,3})?$".
Only non-zero positive integers can be entered: "^+?[1-9][0-9]*$".
Only non-zero negative integers can be entered: "^-[1-9][]0-9"*$.
Only characters with a length of 3 can be entered: "^.{3}$".
You can only enter a string consisting of 26 English letters: "^[A-Za-z]+$".
You can only enter a string consisting of 26 uppercase English letters: "^[A-Z]+$".
You can only enter a string consisting of 26 lowercase English letters: "^[a-z]+$".
You can only enter a string consisting of numbers and 26 English letters: "^[A-Za-z0-9]+$".
You can only enter a string consisting of numbers, 26 English letters or underscores: "^w+$".
Verify user password: "^[a-zA-Z]w{5,17}$" The correct format is: starting with a letter, the length is between 6 and 18, and can only contain characters, numbers and underscores.
Verify whether it contains characters such as ^%&'',;=?$": "[^%&'',;=?$x22]+".
Only Chinese characters can be entered: "^[u4e00-u9fa5]{0 ,}$"
Verify email address: "^w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*$".
Verify InternetURL: "^ http://([w-]+.)+[w-]+(/[w-./?%&=]*)?$".
Verification phone number: "^((d{3,4}-)|d{3.4}-)?d{7,8}$" The correct format is: "XXX-XXXXXXX", "XXXX-XXXXXXXX", "XXX -XXXXXXX", "XXX-XXXXXXXX", "XXXXXXX" and "XXXXXXXX".
Verify ID number (15 or 18 digits): "^d{15}|d{18}$".
Verify the 12 months of a year: "^(0?[1-9]|1[0-2])$" The correct format is: "01"~"09" and "1"~"12".
Verify the 31 days of a month: "^((0?[1-9])|((1|2)[0-9])|30|31)$" The correct format is; "01"~"09 "and"1"~"31". Integer or decimal: ^[0-9]+.{0,1}[0-9]{0,2}$
Only numbers can be entered: "^[0-9]*$".
Only n-digit numbers can be entered: "^d{n}$".
You can only enter a number with at least n digits: "^d{n,}$".
Only m~n digits can be entered:. "^d{m,n}$"
You can only enter numbers starting with zero and non-zero: "^(0|[1-9][0-9]*)$".
Only positive real numbers with two decimal places can be entered: "^[0-9]+(.[0-9]{2})?$".
You can only enter positive real numbers with 1~3 decimal places: "^[0-9]+(.[0-9]{1,3})?$".
Only non-zero positive integers can be entered: "^+?[1-9][0-9]*$".
Only non-zero negative integers can be entered: "^-[1-9][]0-9"*$.
Only characters with a length of 3 can be entered: "^.{3}$".
You can only enter a string consisting of 26 English letters: "^[A-Za-z]+$".
You can only enter a string consisting of 26 uppercase English letters: "^[A-Z]+$".
You can only enter a string consisting of 26 lowercase English letters: "^[a-z]+$".
You can only enter a string consisting of numbers and 26 English letters: "^[A-Za-z0-9]+$".
You can only enter a string consisting of numbers, 26 English letters or underscores: "^w+$".
Verify user password: "^[a-zA-Z]w{5,17}$" The correct format is: starting with a letter, the length is between 6 and 18, and can only contain characters, numbers and underscores.
Verify whether it contains characters such as ^%&'',;=?$": "[^%&'',;=?$x22]+".
Only Chinese characters can be entered: "^[u4e00-u9fa5]{0 ,}$"
Verify email address: "^w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*$".
Verify InternetURL: "^ http://([w-]+.)+[w-]+(/[w-./?%&=]*)?$".
Verification phone number: "^((d{3,4}-)|d{3.4}-)?d{7,8}$" The correct format is: "XXX-XXXXXXX", "XXXX-XXXXXXXX", "XXX -XXXXXXX", "XXX-XXXXXXXX", "XXXXXXX" and "XXXXXXXX".
Verify ID number (15 or 18 digits): "^d{15}|d{18}$".
Verify the 12 months of a year: "^(0?[1-9]|1[0-2])$" The correct format is: "01"~"09" and "1"~"12".
Verify the 31 days of a month: "^((0?[1-9])|((1|2)[0-9])|30|31)$" The correct format is; "01"~"09 "and"1"~"31".
"^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]+$" //A string composed of 26 English letters
"^[A-Z]+$" //Consisted of A string of 26 uppercase English letters
"^[a-z]+$" //A string of 26 lowercase English letters
"^[A-Za-z0-9]+$" //A string consisting of numbers and 26 English letters
"^w+$" //A string consisting of numbers, 26 English letters or underscores
" ^[w-]+(.[w-]+)*@[w-]+(.[w-]+)+$" //email address
"^[a-zA-z]+:// (w+(-w+)*)(.(w+(-w+)*))*(?S*)?$" //url

整数或者小数:^[0-9]+\.{0,1}[0-9]{0,2}$ 
只能输入数字:"^[0-9]*$"。 
只能输入n位的数字:"^\d{n}$"。 
只能输入至少n位的数字:"^\d{n,}$"。 
只能输入m~n位的数字:。"^\d{m,n}$" 
只能输入零和非零开头的数字:"^(0|[1-9][0-9]*)$"。 
只能输入有两位小数的正实数:"^[0-9]+(.[0-9]{2})?$"。 
只能输入有1~3位小数的正实数:"^[0-9]+(.[0-9]{1,3})?$"。 
只能输入非零的正整数:"^\+?[1-9][0-9]*$"。 
只能输入非零的负整数:"^\-[1-9][]0-9"*$。 
只能输入长度为3的字符:"^.{3}$"。 
只能输入由26个英文字母组成的字符串:"^[A-Za-z]+$"。 
只能输入由26个大写英文字母组成的字符串:"^[A-Z]+$"。 
只能输入由26个小写英文字母组成的字符串:"^[a-z]+$"。 
只能输入由数字和26个英文字母组成的字符串:"^[A-Za-z0-9]+$"。 
只能输入由数字、26个英文字母或者下划线组成的字符串:"^\w+$"。 
验证用户密码:"^[a-zA-Z]\w{5,17}$"正确格式为:以字母开头,长度在6~18之间,只能包含字符、数字和下划线。 
验证是否含有^%&'',;=?$\"等字符:"[^%&'',;=?$\x22]+"。 
只能输入汉字:"^[\u4e00-\u9fa5]{0,}$" 
验证Email地址:"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$"。 
验证InternetURL:"^http://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$"。 
验证电话号码:"^(\(\d{3,4}-)|\d{3.4}-)?\d{7,8}$"正确格式为:"XXX-XXXXXXX"、"XXXX-XXXXXXXX"、"XXX-XXXXXXX"、"XXX-XXXXXXXX"、"XXXXXXX"和"XXXXXXXX"。 
验证身份证号(15位或18位数字):"^\d{15}|\d{18}$"。 
验证一年的12个月:"^(0?[1-9]|1[0-2])$"正确格式为:"01"~"09"和"1"~"12"。 
验证一个月的31天:"^((0?[1-9])|((1|2)[0-9])|30|31)$"正确格式为;"01"~"09"和"1"~"31"。整数或者小数:^[0-9]+\.{0,1}[0-9]{0,2}$ 
只能输入数字:"^[0-9]*$"。 
只能输入n位的数字:"^\d{n}$"。 
只能输入至少n位的数字:"^\d{n,}$"。 
只能输入m~n位的数字:。"^\d{m,n}$" 
只能输入零和非零开头的数字:"^(0|[1-9][0-9]*)$"。 
只能输入有两位小数的正实数:"^[0-9]+(.[0-9]{2})?$"。 
只能输入有1~3位小数的正实数:"^[0-9]+(.[0-9]{1,3})?$"。 
只能输入非零的正整数:"^\+?[1-9][0-9]*$"。 
只能输入非零的负整数:"^\-[1-9][]0-9"*$。 
只能输入长度为3的字符:"^.{3}$"。 
只能输入由26个英文字母组成的字符串:"^[A-Za-z]+$"。 
只能输入由26个大写英文字母组成的字符串:"^[A-Z]+$"。 
只能输入由26个小写英文字母组成的字符串:"^[a-z]+$"。 
只能输入由数字和26个英文字母组成的字符串:"^[A-Za-z0-9]+$"。 
只能输入由数字、26个英文字母或者下划线组成的字符串:"^\w+$"。 
验证用户密码:"^[a-zA-Z]\w{5,17}$"正确格式为:以字母开头,长度在6~18之间,只能包含字符、数字和下划线。 
验证是否含有^%&'',;=?$\"等字符:"[^%&'',;=?$\x22]+"。 
只能输入汉字:"^[\u4e00-\u9fa5]{0,}$" 
验证Email地址:"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$"。 
验证InternetURL:"^http://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$"。 
验证电话号码:"^(\(\d{3,4}-)|\d{3.4}-)?\d{7,8}$"正确格式为:"XXX-XXXXXXX"、"XXXX-XXXXXXXX"、"XXX-XXXXXXX"、"XXX-XXXXXXXX"、"XXXXXXX"和"XXXXXXXX"。 
验证身份证号(15位或18位数字):"^\d{15}|\d{18}$"。 
验证一年的12个月:"^(0?[1-9]|1[0-2])$"正确格式为:"01"~"09"和"1"~"12"。 
验证一个月的31天:"^((0?[1-9])|((1|2)[0-9])|30|31)$"正确格式为;"01"~"09"和"1"~"31"。 
可输入形如2008、2008-9、2008-09、2008-9-9、2008-09-09.    ^(\d{4}|(\d{4}-\d{1,2})|(\d{4}-\d{1,2}-\d{1,2}))$ 
邮箱验证正则表达式   \w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)* 
System.Text.RegularExpressions.Match m = System.Text.RegularExpressions.Regex.Match(s, @"您的IP:(?[0-9\.]*)");
一.网络验证应用技巧

1. 验证 E-mail格式

public bool IsEmail(string str_Email)
   {
        return System.Text.RegularExpressions.Regex.IsMatch(str_Email,@"^([\w-\.]+)@((
[−9]1,3\.[−9]1,3\.[−9]1,3\.)|(([\w−]+\.)+))([a−zA−Z]2,4|[−9]1,3)(
?)$");
   }

2. 验证 IP 地址

      public bool IPCheck(string IP)
      {
          string num = "(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)";
          return Regex.IsMatch(IP,("^" + num + "\\." + num + "\\." + num + "\\." + num + "$"));
       }

3. 验证 URL

     public bool IsUrl(string str_url)
     {
        return System.Text.RegularExpressions.Regex.IsMatch(str_url, @"http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?");
     }

 二.  常用数字验证技巧

1. 验证电话号码

   public bool IsTelephone(string str_telephone)
    {
        return System.Text.RegularExpressions.Regex.IsMatch(str_telephone, @"^(\d{3,4}-)?\d{6,8}$");
    }

2. 输入密码条件(字符与数据同时出现)

   public bool IsPassword(string str_password)
    {          
        return System.Text.RegularExpressions.Regex.IsMatch(str_password, @"[A-Za-z]+[0-9]");
    }

3. 邮政编号

   public bool IsPostalcode(string str_postalcode)
    {
        return System.Text.RegularExpressions.Regex.IsMatch(str_postalcode, @"^\d{6}$");
    }

4. 手机号码

    public bool IsHandset(string str_handset)
    {
        return System.Text.RegularExpressions.Regex.IsMatch(str_handset, @"^[1]+[3,5]+\d{9}$");
    }

5. 身份证号

    public bool IsIDcard(string str_idcard)
    {
        return System.Text.RegularExpressions.Regex.IsMatch(str_idcard, @"(^\d{18}$)|(^\d{15}$)");
    }

6. 两位小数

    public bool IsDecimal(string str_decimal)
    {
            return System.Text.RegularExpressions.Regex.IsMatch(str_decimal, @"^[0-9]+(.[0-9]{2})?$");
    }

7. 一年的12个月

    public bool IsMonth(string str_Month)
    {
        return System.Text.RegularExpressions.Regex.IsMatch(str_Month, @"^(0?[[1-9]|1[0-2])$");
    }

8. 一个月的31天

    public bool IsDay(string str_day)
    {
        return System.Text.RegularExpressions.Regex.IsMatch(str_day, @"^((0?[1-9])|((1|2)[0-9])|30|31)$");
    }

9. 数字输入

    public bool IsNumber(string str_number)
     {
        return System.Text.RegularExpressions.Regex.IsMatch(str_number, @"^[0-9]*$");
     }

10. 密码长度 (6-18位)

    public bool IsPasswLength(string str_Length)
    {
        return System.Text.RegularExpressions.Regex.IsMatch(str_Length, @"^\d{6,18}$");
    }

11. 非零的正整数

    public bool IsIntNumber(string str_intNumber)
    {
        return System.Text.RegularExpressions.Regex.IsMatch(str_intNumber, @"^\+?[1-9][0-9]*$");
    }

三. 常用字符验证技巧

1. 大写字母

    public bool IsUpChar(string str_UpChar)
    {
        return System.Text.RegularExpressions.Regex.IsMatch(str_UpChar, @"^[A-Z]+$");
    }

2. 小写字母

    public bool IsLowChar(string str_UpChar)
    {
        return System.Text.RegularExpressions.Regex.IsMatch(str_UpChar, @"^[a-z]+$");
    }

3. 检查字符串重复出现的词

     private void btnWord_Click(object sender, EventArgs e)
     {
            System.Text.RegularExpressions.MatchCollection matches =
 System.Text.RegularExpressions.Regex.Matches(label1.Text, 
@"\b(?<word>\w+)\s+(\k<word>)\b", 
System.Text.RegularExpressions.RegexOptions.Compiled |          
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            if (matches.Count != 0)
            {
                foreach (System.Text.RegularExpressions.Match match in matches)
                {
                    string word = match.Groups["word"].Value;
                    MessageBox.Show(word.ToString(),"英文单词");
                }
            }
            else { MessageBox.Show("没有重复的单词"); }
        }

4. 替换字符串

   private void button1_Click(object sender, EventArgs e)
   {           
        string strResult = System.Text.RegularExpressions.Regex.Replace(textBox1.Text, @"[A-Za-z]\*?", textBox2.Text);
        MessageBox.Show("替换前字符:" + "\n" + textBox1.Text + "\n" + 
"替换的字符:" + "\n" + textBox2.Text + "\n" +  "替换后的字符:" + "\n" + 
strResult,"替换");    }

5. 拆分字符串

  private void button1_Click(object sender, EventArgs e)
   {
        //实例: 甲025-8343243乙0755-2228382丙029-32983298389289328932893289丁
        foreach (string s in System.Text.RegularExpressions.Regex.Split(textBox1.Text,@"\d{3,4}-\d*"))
        {
            textBox2.Text+=s; //依次输出 "甲乙丙丁"
        }
    }

6. 验证输入字母

   public bool IsLetter(string str_Letter)
   {
        return System.Text.RegularExpressions.Regex.IsMatch(str_Letter, @"^[A-Za-z]+$");
   }

7. 验证输入汉字

   public bool IsChinese(string str_chinese)
   {
        return System.Text.RegularExpressions.Regex.IsMatch(str_chinese, @"^[\u4e00-\u9fa5],{0,}$");
   }

8. 验证输入字符串 (至少8个字符)

  public bool IsLength(string str_Length)
  {
        return System.Text.RegularExpressions.Regex.IsMatch(str_Length, @"^.{8,}$");
  }


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn