First, the concept of regular expression:
In computer science, a single string that describes or matches a series of strings that conform to a certain syntactic rule. In many text editors or other tools, regular expressions are often used to retrieve and/or replace text content that conforms to a pattern. Many programming languages support the use of regular expressions for string manipulation.
Second, the use of regular expressions:
Regular expression in asp.net is mainly used to verify the input content, the validation is generally divided into two kinds of client JS authentication, the other is server-side verification
1, JS to the input content validation
Copy Code code as follows:
function Check () {
var match =/^\d$/;
var val = $ ("#txt"). Val ();
if (Match.test (val)) {
Alert ("Test Pass");
}
else {
Alert ("Test does not pass");
}
}
2, C # to verify the format
Copy Code code as follows:
protected void Button1_Click (object sender, EventArgs e)
{
String pattern = @ "\d";
If System.Text.RegularExpressions.Regex.IsMatch (this. TextBox1.Text, pattern))
{
Clientscript.registerclientscriptblock (GetType (), "", "alert (' Verify success! ')", true);
}
Else
{
Clientscript.registerclientscriptblock (GetType (), "", "alert (' Verify unsuccessful! ')", true);
}
}
Rules of Regular expressions
\: Mark the next character as a special character, or a literal character, or a back reference, or a octal escape character, for example, ' n ' matches the character ' n '. ' \ n ' matches a newline character. Sequence ' \ ' matches ' \ ' and ' \ (' Matches ' (".
^: matches the start position of the input string.
$: Matches the end position of the input string.
*: Matches the preceding subexpression 0 or more times. For example, zo* can match "z" and "Zoo". * is equivalent to {0,}.
+: Matches the preceding subexpression one or more times. For example, ' zo+ ' can match "Zo" and "Zoo", but cannot match "Z". + is equivalent to {1,}.
?: matches the preceding subexpression 0 times or once. For example, "Do (es)" can match "do" in "do" or "does". is equivalent to {0,1}.
{N}:n is a non-negative integer. Matches the determined 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}:M and n are non-negative integers, where n <= m. Matches n times at least and matches up to M times. Liu, "o{1,3}" will match the first three o in "Fooooood". ' o{0,1} ' is equivalent to ' o '. Notice that there is no space between the comma and the two number.
?: When the character is immediately following any of the other qualifiers (*, +,?, {n}, {n,}, {n,m}), the matching pattern is not greedy. Non-greedy patterns match as few strings as possible, while the default greedy pattern matches as many of the searched strings as possible. For example, for the string "oooo", ' o+? ' will match a single "O", and ' o+ ' will match all ' o '.
X|y: Matches x or Y. For example, ' Z|food ' can match "z" or "food". ' (z|f) Ood ' matches ' zood ' or ' food '. This one is more important.
[XYZ]: Character set combination. Matches any one of the characters contained. For example, ' [ABC] ' can match ' a ' in ' plain '.
[^XYZ]: Negative character set combination. Matches any characters that are not included. For example, ' [^ABC] ' can match ' P ' in ' plain '.
[A-Z]: Character range. Matches any character within the specified range. For example, ' [A-z] ' can match any lowercase alphabetic character in the range ' a ' to ' Z '.
[^a-z]: a negative character range. Matches any character that is not in the specified range. For example, ' [^a-z] ' can match any character that is not in the range of ' a ' to ' Z '.
\b: Matches a word boundary, which refers to the position between the word and the space. For example, ' er\b ' can match ' er ' in ' never ', but cannot match ' er ' in ' verb '.
\b: Matches a non word boundary. ' er\b ' can match ' er ' in ' verb ', but cannot match ' er ' in ' Never '.
\CX: Matches the control character indicated by X. For example, \cm matches a control-m or carriage return character. The value of x must be one-a-Z or a-Z. Otherwise, c is treated as a literal ' C ' character.
\d: Matches a numeric character. equivalent to [0-9].
\d: Matches a non-numeric character. equivalent to [^0-9].
\f: Matches a page feed character. Equivalent to \x0c and \CL.
\ n: Matches a newline character. Equivalent to \x0a and \CJ.
\ r: Matches a carriage return character. Equivalent to \x0d and \cm.
\s: Matches any white space character, including spaces, tabs, page breaks, and so on. equivalent to [\f\n\r\t\v].
\s: Matches any non-white-space character. equivalent to [^ \f\n\r\t\v].
\ t: matches a tab character. Equivalent to \x09 and \ci.
\v: Matches a vertical tab. Equivalent to \x0b and \ck.
\w: Matches any word character that includes an underscore. Equivalent to ' [a-za-z0-9_] '.
\w: matches any non word character. Equivalent to ' [^a-za-z0-9_] '.
\xn: Matches n, where n is the hexadecimal escape value. The hexadecimal escape value must be a determined two digits long. For example, ' \x41 ' matches ' A '. ' \x041 ' is equivalent to ' \x04 ' & ' 1 '. You can use ASCII encoding in regular expressions ...
\num: Matches num, where num is a positive integer. A reference to the match that was obtained. For example, ' (.) \1 ' matches two consecutive identical characters.
\ n: Identifies a octal escape value or a back reference. N is a back reference if you have at least N obtained subexpression before. Otherwise, if n is an octal number (0-7), then N is an octal escape value.
\NM: Identifies a octal escape value or a back reference. NM is a \nm if at least one of the preceded by the atleast nm is acquired before the If there are at least N fetches before \nm, then N is a back reference followed by a literal m. If all the preceding conditions are not satisfied, if both N and M are octal digits (0-7), then \nm will match octal escape value nm.
\NML: If n is an octal number (0-3), and both M and L are octal digits (0-7), then the octal escape value NML is matched.
\un: Matches n, where N is a Unicode character represented in four hexadecimal digits. For example, \u00a9 matches the copyright symbol (?).
Some verification on the Internet
E-Mail: ^ ([a-za-z0-9]+[_|\_|\.]?) *[a-za-z0-9]+@ ([a-za-z0-9]+[_|\_|\.]?) *[a-za-z0-9]+\. [A-za-z] {2,3}$
Phone or cell phone: ^189\d{8}$ | (^13\d{9}$) | (^15\d{9}$) | (^\d{8}$) | (^0574-\d{8}$
"^\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+)? $"//nonnegative floating-point number (positive float + 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 of 26 English letters
"^[a-z]+$"//A string of 26 uppercase letters
"^[a-z]+$"///a string consisting of 26 lowercase letters
"^[a-za-z0-9]+$"//A string of numbers and 26 English letters
"^\w+$"//A string of numbers, 26 English letters, or underscores
"^[\w-]+ (\.[ \w-]+) *@[\w-]+ (\.[ \w-]+) +$ "//email address
"^[a-za-z]+://(\w+ (-\w+) *) (\. ( \w+ (-\w+) *)) * (\?\s*) $ "//url
/^13\d{9}$/gi cell phone number Regular expression
Regular expression--verification of mobile phone number: 13[0-9]{9}
To achieve the mobile phone number 86 or +86 before: ^ (\+86) | (86))? (\d{9}$)
The phone number and mobile number are verified simultaneously: (^ (\d{3,4}-) \d{7,8}) $| (13[0-9]{9})
To extract the network links in the information: (h| H) (r| R) (e| E) (f| F) *= * (' | ')? (\w|\\|\/|\.) +('|"| *|>)?
Message address in the extraction information: \w+ ([-+.] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] \w+) *
Extract picture links in information: (s| S) (r| R) (c| C) *= * (' | ')? (\w|\\|\/|\.) +('|"| *|>)?
Extract the IP address in the information: (\d+) \. (\d+) \. (\d+) \. (\d+)
China Mobile phone number to extract information: (*0*13\D{9)
Extract Chinese fixed phone number in information: (\ (\d{3,4}\) |\d{3,4}-|\s)? \d{8}
Extract the Chinese phone number (including mobile and landline) in the information:(\ (\d{3,4}\) |\d{3,4}-|\s)? \d{7,14}
China Post Code in Extract information: [1-9]{1} (\d+) {5}
Chinese identity card number in the extraction of information: \d{18}|\d{15}
To extract an integer from the information: \d+
Extract floating-point numbers (that is, decimals) in the information: (-?\d*) \.? \d+
Extract any number in the information: (-?\d*) (\.\d+)?
Extract the Chinese string in the information: [\u4e00-\u9fa5]*
Extract a double-byte string (kanji) from the information: [^\x00-\xff]*
Regular expression rules look very complex, as long as the patience to find out the rules, integration is often used, work will be more effective.