Zzc # Regular Expression Summary

Source: Internet
Author: User
C # Regular Expression Summary

Address: http://www.cnblogs.com/maxianghui/archive/2006/05...

Really good. Thanks to this friend.

Only numbers are allowed: "^ [0-9] * $ ".
You can only enter n digits: "^" d {n} $ ".
You can only enter at least N digits: "^" d {n,} $ ".
Only M ~ can be input ~ N-digit :. "^" D {m, n} $"
Only numbers starting with zero and non-zero can be entered: "^ (0 | [1-9] [0-9] *) $ ".
Only positive numbers with two decimal places can be entered: "^ [0-9] + (. [0-9] {2 })? $ ".
Only 1 ~ Positive number of three decimal places: "^ [0-9] + (. [0-9] {1, 3 })? $ ".
Only a non-zero positive integer can be entered: "^" +? [1-9] [0-9] * $ ".
You can only enter a non-zero negative integer: "^"-[1-9] [] 0-9 "* $.
Only 3 characters 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 letters: "^ [A-Z] + $ ".
You can only enter a string consisting of 26 lower-case English letters: "^ [A-Z] + $ ".
You can only enter a string consisting of a number and 26 English letters: "^ [A-Za-z0-9] + $ ".
You can only enter a string consisting of digits, 26 English letters, or underscores: "^" W + $ ".
Verify the User Password: "^ [A-Za-Z]" W {5, 17} $ ". The correct format is: start with a letter, with a length of 6 ~ It can only contain characters, numbers, and underscores.
Check whether ^ % & ',; =? $ "" And other characters: "[^ % & ',; =? $ "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} $" correct format: "XXX-XXXXXXX", "XXXX-XXXXXXXX", "XXX-XXXXXXX", "XXX-XXXXXXXX", "xxxxxxx" and "XXXXXXXX ".
Verify the ID card number (15 or 18 digits): "^" d {15} | "d {18} $ ".
12 months of verification: "^ (0? [1-9] | 1 [0-2]) $ "the correct format is:" 01 "~ "09" and "1 "~ "12 ".
31 days of verification for a month: "^ (0? [1-9]) | (1 | 2) [0-9]) | 30 | 31) $ "the correct format is;" 01 "~ "09" and "1 "~ "31 ".
Use regular expressions to restrict text box input in a webpage form:

Use
The regular expression can only contain Chinese characters: onkeyup = "value = value. Replace (/[^" u4e00-"u9fa5]/g ,'')"
Onbeforepaste = "clipboardData. setdata ('text', clipboardData. getdata ('text'). Replace (/[^" u4e00-"u9fa5]/g ,''))"

Only full-width characters can be entered using regular expressions: onkeyup = "value = value. Replace (/[^" uff00-"Uffff]
/G ,'')"
Onbeforepaste = "clipboardData. setdata ('text', clipboardData. getdata ('text'). Replace (/[^" uff00-"Uffff]/g ,''))"

Use a regular expression to limit that only numbers can be entered: onkeyup = "value = value. Replace (/[^" d]/g ,'')
"Onbeforepaste =" clipboardData. setdata ('text', clipboardData. getdata ('text'). Replace (/[^ "d]/g ,''))"

Only numbers and English characters can be entered with a regular expression: onkeyup = "value = value. Replace (/[" W]/g ,'')
"Onbeforepaste =" clipboardData. setdata ('text', clipboardData. getdata ('text'). Replace (/[^ "d]/g ,''))"

Javascript programs that extract file names from URLs using regular expressions. the following result is page1.

S = "http://www.9499.net/page1.htm"
S = S. Replace (/(. * "/) {0,} ([^".] +). */ig, "$2 ")
Alert (s)

Match double-byte characters (including Chinese characters): [^ "x00-" xFF]

Application: Calculate the length of a string (two-byte length Meter 2, ASCII character meter 1)

String. Prototype. Len = function () {return this. Replace ([^ "x00-" xFF]/g, "AA"). length ;}

Regular Expression matching empty rows: "N [" s |] * "R

Regular Expressions matching HTML tags:/<(. *)>. * <"/" 1 >|< (. *) "/>/

Regular Expression matching spaces at the beginning and end: (^ "s *) | (" s * $)

String. Prototype. Trim = function ()
{
Return this. Replace (/(^ "s *) | (" s * $)/g ,"");
}

Use regular expressions to break down and convert IP addresses:

The following is a javascript program that uses regular expressions to match IP addresses and convert IP addresses to corresponding values:

Function ip2v (IP)
{
Re =/("d +)". ("d +)/g // The regular expression that matches the IP address
If (Re. Test (IP ))
{
Return Regexp. $1 * Math. Pow (255) + Regexp. $2 * Math. Pow () + Regexp. $3 * + Regexp. $4*1
}
Else
{
Throw new error ("not a valid IP address! ")
}
}

However, if the above program does not use regular expressions, it may be easier to directly use the split function to separate them. The program is as follows:

VaR IP = "10.100.0000168"
IP = IP. Split (".")
Alert ("the IP value is: "+ (IP [0] * 255*255*255 + IP [1] * 255*255 + IP [2] * 255 + IP [3] * 1 ))
Symbol explanation:

Character
Description

"
Mark the next character as a special character, a literal character, or a backward reference, or an octal escape character. For example, 'n' matches the character "N ". '"N' matches a line break. The sequence '""' matches "and" "(" matches "(".

^
Matches the start position of the input string. If the multiline attribute of the Regexp object is set, ^ matches the position after '"N' or'" R.

$
Matches the end position of the input string. If the multiline attribute of the Regexp object is set, $ also matches the location before '"N' or'" R.

*
Matches the previous subexpression zero or multiple times. For example, Zo * can match "Z" and "Zoo ". * Is equivalent to {0 ,}.

+
Match the previous subexpression once or multiple times. For example, 'Zo + 'can match "zo" and "Zoo", but cannot match "Z ". + Is equivalent to {1 ,}.

?
Match the previous subexpression zero or once. For example, "Do (ES )? "Can match" do "in" do "or" does ".? It is equivalent to {0, 1 }.

{N}
N is a non-negative integer. Match n times. For example, 'O {2} 'cannot match 'O' in "Bob", but can match two o in "food.

{N ,}
N is a non-negative integer. Match at least N times. For example, 'O {2,} 'cannot match 'O' in "Bob", but can match all o in "foooood. 'O {1,} 'is equivalent to 'o + '. 'O {0,} 'is equivalent to 'o *'.

{N, m}
Both m and n are non-negative integers, where n <= m. Match at least N times and at most m times. For example, "O {1, 3}" matches the first three o in "fooooood. 'O {0, 1} 'is equivalent to 'o? '. Note that there must be no space between a comma and two numbers.

?
When
This character is followed by any other delimiter (*, + ,?, {N}, {n ,}, {n, m })
Later, the matching mode is non-greedy. The non-Greedy mode matches as few searched strings as possible, while the default greedy mode matches as many searched strings as possible. For example
"Oooo", 'O +? 'Will match a single "O", and 'O +' will match all 'O '.

.
Matches any single character except "N. To match any character including '"N', use a pattern like' [." N.

(Pattern)
Match pattern and obtain this match. The obtained match can be obtained from the generated matches set. The submatches set is used in VBScript, and $0… is used in JScript... $9 attribute. To match the parentheses, use '"(' or '")'.

(? : Pattern)
Horse
Match results are not obtained, that is, this is a non-get match and is not stored for future use. This is using the "or" character (|)
It is useful to combine all parts of a pattern. For example, 'industr (? : Y | ies'
A simpler expression.

(? = Pattern)
Forward pre-query, in any match Pattern
To start from the string. This is a non-get match, that is, the match does not need to be obtained for future use. For example, 'windows
(? = 95 | 98 | nt | 2000) 'can match "Windows" in "Windows 2000", but cannot match "Windows 3.1"
In
"Windows ". Pre-query does not consume characters, that is, after a match occurs, the next matching search starts immediately after the last match, instead of starting after the pre-query characters.

(?! Pattern)
Negative
Forward query, where no pattern matches
To start from the string. This is a non-get match, that is, the match does not need to be obtained for future use. For example, 'windows
(?! 95 | 98 | nt | 2000) 'can match "Windows" in "Windows 3.1", but cannot match "Windows 2000"
In
"Windows ". Pre-query does not consume characters. That is to say, after a match occurs, the next matching search starts immediately after the last match, instead of starting after the pre-query characters.

X | y
Match X or Y. For example, 'z | food' can match "Z" or "food ". '(Z | f) Ood' matches "zood" or "food ".

[Xyz]
Character Set combination. Match any character in it. For example, '[ABC]' can match 'A' in "plain '.

[^ XYZ]
Negative value character set combination. Match any character not included. For example, '[^ ABC]' can match 'p' in "plain '.

[A-Z]
Character range. Matches any character in the specified range. For example, '[A-Z]' can match any lowercase letter in the range of 'A' to 'Z.

[^ A-Z]
Negative character range. Matches any character that is not within the specified range. For example, '[^ A-Z]' can match any character that is not in the range of 'A' to 'Z.

"B
Match A Word boundary, that is, the position between a word and a space. For example, 'er "B 'can match 'er' in" never ", but cannot match 'er 'in" verb '.

"B
Match non-word boundary. 'Er "B 'can match 'er' in" verb ", but cannot match 'er 'in" never '.

"CX
Match the control characters specified by X. For example, "cm" matches a control-M or carriage return character. The value of X must be either a A-Z or a-Z. Otherwise, C is treated as an original 'C' character.

"D
Match a numeric character. It is equivalent to [0-9].

"D
Match a non-numeric character. It is equivalent to [^ 0-9].

"F
Match a form feed. It is equivalent to "x0c" and "Cl.

"N
Match A linefeed. It is equivalent to "x0a" and "CJ.

"R
Match a carriage return. It is equivalent to "x0d" and "cm.

"S
Matches any blank characters, including spaces, tabs, and page breaks. It is equivalent to ["F" N "R" T "V].

"S
Match any non-blank characters. It is equivalent to [^ "F" N "R" T "V].

"T
Match a tab. It is equivalent to "x09" and "Ci.

"V
Match a vertical tab. It is equivalent to "x0b" and "ck.

"W
Match any word characters that contain underscores. It is equivalent to '[A-Za-z0-9 _]'.

"W
Match any non-word characters. It is equivalent to '[^ A-Za-z0-9 _]'.

"XN
Match n, where N is the hexadecimal escape value. The hexadecimal escape value must be determined by the length of two numbers. For example, '"x41' matches" ". '"X041' is equivalent to '" x04' & "1 ". The regular expression can use ASCII encoding ..

"Num
Matches num, where num is a positive integer. References to the obtained matching. For example, '(.) "1' matches two consecutive identical characters.

"N
Identifies an octal escape value or a backward reference. If there are at least N obtained subexpressions before "N", n is a backward reference. Otherwise, if n is an octal digit (0-7), n is an octal escape value.

"Nm
Mark
Identifies an octal escape value or a backward reference. If there are at least nm subexpressions before "Nm", the NM is a backward reference. If there is at least N before "Nm ",
N is a backward reference followed by text M. If none of the preceding conditions are met, if n and m are octal digits (0-7), then "nm
Match the octal escape value nm.

"NML
If n is an octal number (0-3) and M and l are Octal numbers (0-7), the octal escape value NML is matched.

"UN
Match n, where n is a Unicode character represented by four hexadecimal numbers. For example, "u00a9 matches the copyright symbol (?).

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.