Common regular expressions in. net development

Source: Internet
Author: User
Tags character set control characters html tags numeric lowercase regular expression

You are familiar with the simplest regular expressions, that is, text strings. A specific string can be described by the text itself; regular expression patterns like foo can precisely match the input string foo. In this example, the following input is also matched: the foo d was quite tasty. If you want exact match, this may not be the expected result.

Of course, using regular expressions to match exact strings that are equal to itself is meaningless and does not reflect the true effect of regular expressions. What should I do if I do not search for foo, but all the words starting with f or all three letters? At present, this is beyond the reasonable range of text strings. We need to study regular expressions in more depth. Below is a text expression example and some matching input.

 

"^ 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 consisting of 26 English letters
"^ [A-z] + $" // a string consisting of 26 uppercase letters
"^ [A-z] + $" // a string consisting of 26 lowercase letters
"^ [A-Za-z0-9] + $" // string consisting of digits and 26 letters
"^ W + $" // a string consisting of digits, 26 letters, or underscores
"^ [W-] + (. [w-] +) * @ [w-] + (. [w-] +) + $" // email address
"^ [A-za-z] +: // (w + (-w +) *) (. (w + (-w + )*))*(? S *)? $ "// Url
/^ (D {2} | d {4})-(0 ([1-9] {1}) | (1 [1 | 2]) -([0-2] ([1-9] {1}) | (3 [0 | 1]) $ // year-month-day
/^ (0 ([1-9] {1}) | (1 [1 | 2]) /([0-2] ([1-9] {1}) | (3 [0 | 1]) /(d {2} | d {4}) $ // month/day/year
"^ ([W-.] +) @ ([0-9] {1, 3 }. [0-9] {1, 3 }. [0-9] {1, 3 }.) | ([w-] + .) +) ([a-za-z] {2, 4} | [0-9] {1, 3}) (]?) $ "// Emil
"(D + -)? (D {4 }-? D {7} | d {3 }-? D {8} | ^ d {7, 8}) (-d + )? "// Phone number
"^ (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]) $ "// IP address

Regular Expression Matching Chinese characters: [u4e00-u9fa5]
Match double byte characters (including Chinese characters): [^ x00-xff]
Regular expression for matching empty rows: n [s |] * r
Regular expressions matching html tags:/<(. *)>. * </1> | <(. *)/>/
Regular expression matching the first and last Spaces: (^ s *) | (s * $)
Regular Expression Matching the email address: w + ([-+.] w +) * @ w + ([-.] w + )*. w + ([-.] w + )*
Regular Expression Matching url: ^ [a-za-z] +: // (w + (-w + )*)(. (w + (-w + )*))*(? S *)? $
Match account validity (starting with a letter, may be 5-16 bytes, may be an alphanumeric underline): ^ [a-za-z] [A-Za-z0-9 _] {} $
Match Chinese phone number: (d {3}-| d {4 }-)? (D {8} | d {7 })?
Match Tencent QQ number: ^ [1-9] * [1-9] [0-9] * $

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, '')" maid = "clipboarddata. setdata ('text', clipboarddata. getdata ('text '). replace (/[^ u4e00-u9fa5]/g ,''))"

Use a regular expression to limit that only full-width characters can be entered: required nkeyup = "value = value. replace (/[^ uff00-uffff]/g, '')" maid = "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 ,''))"


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
/^ [T] * $/"^ [t] * $" 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.
/<(. *)>. * </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. 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 *'.
Both {n, m} 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 (*, + ,?, The matching mode after {n}, {n ,}, {n, m}) 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 '.
. Match any single character except "n. To match any character including 'n', use a pattern like '[. n.
(Pattern) matches pattern and obtains 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 parentheses, use '(' or ')'.
(? : Pattern) matches pattern but does not get the matching result. That is to say, 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: matches the search string at the beginning of any string that matches pattern. 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" 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: matches the search string at the beginning of any string that does not match pattern. 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 matches 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] combination of negative character sets. 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 matches a word boundary, that is, the position between a word and a space. For example, 'erb' can match 'ER' in "never", but cannot match 'ER' in "verb '.
B matches non-word boundaries. 'Erb' can match 'ER' in "verb", but cannot match 'ER' in "never '.
Cx matches the control characters specified by x. For example, cm matches a control-m or carriage return character. The value of x must be a-z or one of a-z. Otherwise, c is treated as an original 'C' character.
D matches a numeric character. It is equivalent to [0-9].
D. Match a non-numeric character. It is equivalent to [^ 0-9].
F matches a form feed. It is equivalent to x0c and cl.
N matches a linefeed. It is equivalent to x0a and cj.
R matches 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 [fnrtv].
S matches any non-blank characters. It is equivalent to [^ fnrtv].
T matches a tab. It is equivalent to x09 and ci.
V matches a vertical tab. It is equivalent to x0b and ck.
W matches any word characters that contain underscores. It is equivalent to '[A-Za-z0-9 _]'.
W matches any non-word characters. It is equivalent to '[^ A-Za-z0-9 _]'.
Xn matches 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 backward referenced. Otherwise, if n is an octal digit (0-7), n is an octal escape value.
Nm identifies an octal escape value or a backward reference. If at least one child expression is obtained before nm, then nm is backward referenced. If at least n records are obtained 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 numbers (0-7), nm matches the octal escape value nm.
If n is an octal digit (0-3) and m and l are octal numerals (0-7), nml matches the octal escape value nml.
Un matches n, where n 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 '& brvbar;', indicating "or" operation:

"Hi & brvbar; hello": indicates that a string contains "hi" or "hello ";
"(B & brvbar; cd) ef": "bef" or "cdef ";
"(A & brvbar; 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 & brvbar; B ");
"[A-d]": indicates that a string contains one of the lower-case 'A' to 'D' (equivalent to "a & brvbar; B & brvbar; c & brvbar; 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 a verbatim expression, it must be in "^. $ () & brvbar; * +? {"Add the escape character'' before these characters ''.

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.