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. Used in hidden code. This method applies 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. Regular Expressions matching HTML tags: <(. *)>. * </1> | <(. *)/> 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 rows: [s |] *
21. China Post code: [1-9] d {5 }(?! D)
22. Chinese mobile phone: (86) * 0 * 13d {9}
23. Chinese phone number (including mobile phone number) :( (d {3, 4}) | d {3, 4}-| s )? D {7, 14}
24. Number :(-? D *) (. d + )?
25. Double Byte Character: [^ 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 User Password: "^ [a-zA-Z] w {5 ,}$" correct format: starts with a letter and ranges from 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 ,''))"
Javascript programs that extract file names from URLs using regular expressions. 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: [s |] *
Regular Expressions matching HTML tags:/<(. *)>. * </1> | <(. *)/>/
Regular Expression matching the first and last spaces: (^ 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! ")
}
}
Certificate certificate ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
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 matching
/^ [] * $/"^ [] * $" Matches a blank row.
/D {2}-d {5}/"d {2}-d {5}" verify that an ID number is composed of two digits, A hyphen and a five-digit combination.