Two usage methods:
1. Add the regular expression verification control regularexpression_r_rvalidator to the text input box. This method is applicable to webform.
In the validationexpression_r_r option, enter the verification rule.
In the controltovalidate option, select the control to verify.
In the errormesage option, enter the information to be displayed if the verification fails.
2. HideCodeThis method is applicable to winform and webform.
In this method, you must first add the namespace declaration:
Using system. Text. regularexpression_r_rs;
Then declare the verification rules in the method:
String s_reg = @ "^ \ D + $ ";
Declare the string to be verified:
String S = textbox1.text;
Create an instance and pass the verification rules as parameters to the instance. initialize the class:
RegEx Reg = new RegEx (s_reg );
Then, the instance is used to verify the string to be verified. The returned result is a bool value:
If (Reg. ismatch (s ))
{
MessageBox. Show ("It's OK ~ ");
}
Else
{
MessageBox. Show ("It's bad ~ ");
}
Example of a regular expression ::
C # regular expression (1)
1. MatchHtmlThe marked Regular Expression: <(. *)>. * <\/\ 1> | <(. *) \/> For example: <title> </title>
2. Positive Integer: ^ [0-9] * [1-9] [0-9] * $
3. negative integer: ^-[0-9] * [1-9] [0-9] * $
4. Integer :-? \ D + $
5. positive floating point number: ^ ([0-9] + \. [0-9] * [1-9] [0-9] *) | ([0-9] * [1-9] [0-9] * \. [0-9] +) | ([0-9] * [1-9] [0-9] *) $
6. negative floating point number: ^ (-([0-9] + \. [0-9] * [1-9] [0-9] *) | ([0-9] * [1-9] [0-9] * \. [0-9] +) | ([0-9] * [1-9] [0-9] *) $
7. Floating Point: ^ (-? \ D +) (\. \ D + )? $
8.26 English characters (Case Insensitive): ^ [A-Za-Z] + $
9.26 uppercase letters: ^ [A-Z] + $
10.26 lowercase English letters: ^ [A-Z] + $
11. combination of numbers and 26 English letters: ^ [A-Za-z0-9] + $
12. Mix the numbers with 26 English letters and underscores: ^ \ W + $
13. email Address: \ W + ([-+.] \ W +) * @ \ W + ([-.] \ W + )*\. \ W + ([-.] \ W +) * H
14. URL: ^ [A-Za-Z] +: // (\ W + (-\ W + )*)(\. (\ W + (-\ W + )*))*(\? \ S *)? $
15. China phone: (d {3, 4}) | D {3, 4 }-)? D {8888888} (-d {3}) * For example: 021-0515, 88888888-88888888, or 021-888-
16. It must start with a letter and may be 5-16 bytes long and may contain letters, numbers, and underscores (_): ^ [A-Za-Z] [a-zA-Z0-9 _] {6, 20} $
17. QQ: ^ [1-9] * [1-9] [0-9] * $
18. IP Address: ^ (d {1, 2} | 1dd | 2 [0-4] d | 25 [0-5]). (d {1, 2} | 1dd | 2 [0-4] d | 25 [0-5]). (d {1, 2} | 1dd | 2 [0-4] d | 25 [0-5]). (d {1, 2} | 1dd | 2 [0-4] d | 25 [0
-5]) $
19. spaces at the beginning and end: (^ \ s *) | (\ s * $)
20. Empty line: \ n [\ s |] * \ r
21. China Post code: [1-9] \ D {5 }(?! \ D)
22. Chinese mobile phone: (86) * 0*13 \ D {9}
23. China phone (including mobile phone) :( \ (\ D {3, 4} \) | \ D {3, 4}-| \ s )? \ D {7, 14}
24. Number :(-? \ D *) (\. \ D + )?
25. double byte characters: [^ \ x00-\ xFF] *
26. Filter unsafe characters: ^ ([A-Z] * | [A-Z] * | \ D * | [-_\~! @ # \ $ % \ ^ & \ * \. \ (\) \ [\] \ {\} <> \? \/\ '\ "] *) |. {0, 5}) $ | \ s
27. date: (19) {1} | (20) {1}) d {2}) | D {2 }) [01] {1} d {1} [0-3] {1} d {1} e. g.: 20060518
C # Regular Expression (2)
Only numbers are allowed: "^ [0-9] * $ ".
Only n digits can be entered: "^ \ 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] * $ ".
Only a non-zero negative integer can be entered: "^ \-[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 ,}$". 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:
You can only enter Chinese characters using regular expressions: onkeyup = "value = value. replace (/[^ \ u4e00-\ u9fa5]/g, '')" onbeforepaste = "clipboardData. setdata ('text', clipboardData. getdata ('text '). replace (/[^ \ u4e00-\ u9fa5]/g ,''))"
You can only enter the full-width characters: 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 ,''))"
You can only enter numbers and English letters using regular expressions: onkeyup = "value = value. replace (/[\ W]/g, '')" onbeforepaste = "clipboardData. setdata ('text', clipboardData. getdata ('text '). replace (/[^ \ D]/g ,''))"
Extract the JavaScript code of the file name from the URL address using a regular expression.Program, The following result is page1
The following is a reference clip:
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)
The following is a reference clip:
String. Prototype. Len = function () {return this. Replace ([^ \ x00-\ xFF]/g, "AA"). length ;}
Regular Expression for matching empty rows: \ n [\ s |] * \ r
Regular Expressions matching HTML tags:/<(. *)>. * <\/\ 1> | <(. *) \/>/
Regular Expression matching spaces at the beginning and end: (^ \ s *) | (\ s * $)
The following is a reference clip:
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:
The following is a reference clip:
Function ip2v (IP)
{
Re =/(\ D +) \. (\ D +)/g // Regular Expression matching IP addresses
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! ")
}
}
Basic knowledge of Regular Expressions
A regular expression is a text mode consisting of common characters (such as characters A to Z) and special characters (such as metacharacters. This mode describes one or more strings to be matched when searching the text subject. A regular expression is used as a template to match a character pattern with the searched string. For example:
| JScript |
VBScript |
Match |
| /^ \ [\ T] * $/ |
"^ \ [\ T] * $" |
Matches a blank row. |
| /\ D {2}-\ D {5 }/ |
"\ D {2}-\ D {5 }" |
Verify that an ID number consists of a 2-digit, a hyphen, and a 5-digit number. |
| /<(. *)>. * <\/\ 1>/ |
"<(. *)>. * <\/\ 1>" |
Matches an HTML Tag. |
The following table shows a complete list of metacharacters and their behaviors in the context of a regular expression:
| 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. IfRegexpObjectMultilineProperty, ^ matches the position after '\ n' or' \ R. |
| $ |
Matches the end position of the input string. IfRegexpObjectMultilineAttribute, $ also matches the position 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} |
NIs a non-negative integer. MatchedNTimes. For example, 'O {2} 'cannot match 'O' in "Bob", but can match two o in "food. |
| {N,} |
NIs a non-negative integer. At least matchNTimes. 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} |
MAndNAll are non-negative integers, whereN<=M. Least matchNTimes and most matchingMTimes. 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}) The matching mode is not 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, for strings "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) |
MatchPatternAnd obtain the matching. The obtained match can be obtained from the generated matches set. It is used in VBScript.SubmatchesSet, which is used in JScript$0...$9Attribute. To match the parentheses, use '\ (' or '\)'. |
| (? :Pattern) |
MatchPatternBut does not get the matching result, that is, this is a non-get match and is not stored for future use. This is useful when you use the "or" character (|) to combine each part of a pattern. For example, 'industr (? : Y | ies) is a simpler expression than 'industry | industries. |
| (? =Pattern) |
Forward pre-query, in any matchPatternTo 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"Windows" in 2000 ", but cannot match" Windows "in" Windows 3.1 ". 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 pre-query, in any MismatchPatternTo 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" in "Windows 2000 ". 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 |
MatchXOrY. 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 |
MatchingXThe specified control character. For example, \ cm matches a control-M or carriage return character.XMust be a A-Z or one of 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 |
MatchN, WhereNIt is a 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 |
MatchNum, WhereNumIs 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 \NAt leastNObtained subexpressionsNIs backward reference. Otherwise, ifNIs an octal digit (0-7 ),NIt is an octal escape value. |
| \Nm |
Identifies an octal escape value or a backward reference. If \NmAt leastNmTo obtain the subexpressionNmIs backward reference. If \NmAt leastNNIs followed by textM. If none of the preceding conditions are metNAndMAll are Octal numbers (0-7), then \NmMatch the octal escape ValueNm. |
| \NML |
IfNIt is an octal digit (0-3) andMAndLIf the values are Octal numbers (0-7), the octal escape value is matched.NML. |
| \ UN |
MatchN, WhereNIt is a Unicode character represented by four hexadecimal numbers. For example, \ u00a9 matches the copyright symbol (). |
The following are examples:
"^ The": indicates all strings starting with "the" ("there", "The cat", etc );
"Of Despair $": indicates the string ending with "of despair;
"^ ABC $": indicates that the start and end of the string are "ABC"-Haha, only "ABC" itself;
"Notice": indicates any string containing "notice.
'*', '+' And '? 'These three symbols indicate the number of repeated occurrences of one or more characters. They indicate "No or
"More", "one or more", and "none or one ". The following are examples:
"AB *": indicates that a string has one a followed by zero or several B. ("A", "AB", "abbb ",......);
"AB +": indicates that a string is followed by at least one B or more;
"AB? ": Indicates that a string has one a followed by zero or one B;
"? B + $ ": indicates that there are zero or one a followed by one or several B at the end of the string.
You can also use a range enclosed in braces to indicate the range of repeated times.
"AB {2}": indicates that a string has a followed by two B ("ABB ");
"AB {2,}": indicates that a string contains at least two B strings;
"AB {3, 5}": indicates that a string has 3 to 5 B following.
Note that you must specify the lower limit of the range (for example, "{0, 2}" instead of "{, 2 }"). Also, you may have noticed that '*', '+' and
'? 'Is equivalent to "{0,}", "{1,}", and "{0, 1 }".
There is also a 'region', which indicates "or" Operation:
"Hi, hello": indicates that a string contains "hi" or "hello ";
"(B effeccd) EF": "BEF" or "cdef ";
"(A between B) * C": represents a string of "A" "B" mixed strings followed by a "C ";
'.' Can replace any character:
"A. [0-9]": indicates that a string has a "A" followed by an arbitrary character and a number;
"^. {3 }$": represents a string of any three characters (Length: 3 characters );
Square brackets indicate that certain characters can appear at a specific position in a string:
"[AB]": indicates that a string has a "A" or "B" (equivalent to "A between B ");
"[A-d]": indicates that a string contains one of the lower-case 'A' to 'D' (equivalent to "A character B character C character D" or "[ABCD]");
"^ [A-Za-Z]": a string that starts with a letter;
"[0-9] %": indicates a digit before the percent sign;
", [A-zA-Z0-9] $": represents a string ending with a comma followed by a letter or number.
You can also use '^' in square brackets to indicate unwanted characters. '^' should be the first character in square brackets. (For example, "% [^ A-Za-Z] %" table
The two percentage signs should not contain letters ).
For the word-by-word expression, it must be in "^. $ () between * +? {\ "Are preceded by the transfer character '\'.
Note that escape characters are not required in square brackets.