C # Regular Expressions

Source: Internet
Author: User

The regular expression wildcard is called a meta-character

1>. Any single character other than \ n
2> [] means taking any one of the characters in brackets
3> ^ match in brackets to reverse, outside match with what start
4> | Or
5> () Priority or define extraction Group


6> * Qualifier indicates that the preceding expression can appear 0 or more times, optional, can be less
7> + qualifier appears at least 1 or more times
8>? Qualifier indicates one occurrence or 0 occurrences
9> {} Qualifier indicates the number of occurrences of the preceding formal expression
10>{N,M} The number of occurrences of the regular expression preceding the qualifier qualifier is at least between >=n and <=m
11>{n,} The number of occurrences of the regular expression preceding the qualifier qualifier is at least <=n times
12> $ represents the end of a match, such as xyz$ represents any string ending with xyz, with the ^ mate qualifying fixed characters, such as ^xyz$

Regular expressions are represented in a special way (for the compiler to distinguish between escaping, in front of \ should be added @, such as @ "^\d{6}$", etc.):
\d represents any number of 0~9
\d is the opposite of \d, indicating any number of non-0~9

^a[0-9]b$ can be written as ^a\db$, where \d represents the meaning of any number in the 0~9, and capital \d is equivalent to negation, indicating the reverse side of \d
such as [^0-9]b can be written as \db

\s represents whitespace characters, such as spaces and carriage returns, that are not visible
\s the opposite of \s, representing non-whitespace characters

\w denotes numbers, letters, underscores or kanji, which can be any character that makes up a word, except% & #@! $ and other characters. [a-za-z0-9_ Kanji]

\w non-\w, equivalent to [^\w]%

----------------------------------------------------------------------------------
Regular expressions are used by the combination of the above different meta-characters to achieve the matching character.
The result is greater convenience for programming.
Use the Regex class (under the System.Text.RegularExpressions namespace) to handle formal expressions in. Net.
Among the commonly used member methods are:
----------------------------------------------------------------------------------
Regex.IsMatch ()//Determine if a string matches a regular expression
Regex.match ()//extract A
Regex.Matches ()//Extract multiple
Regex.Replace ()//string substitution, replacing all strings that match regular expressions with corresponding characters

Example: e-mail verification @ "^[a-za-z0-9][a-za-z0-9_\." [Email protected] [A-za-z0-9]+ (\.[ a-za-z]+) +$ "
6-bit ZIP code ^[0-9]{6}$
15-bit or 18-bit pure digital ID number ^ ([0-9]{15}|[ 0-9]{18}) $
or ^ (\d{15}|\d{18}) $
or ^\d{15}$|^\d{18}$

Other common C # regular expressions!

"^\d+$"//nonnegative 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 + 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 in English
"^[a-z]+$"//String consisting of 26 English letters in lowercase
"^[a-za-z0-9]+$"//string consisting of a number and 26 English letters
"^\w+$"//A string consisting of numbers, 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

Yyyy-mm-dd basically takes the leap year and the February and so on.
^ ((((1[6-9]| [2-9]\d) \d{2})-(0?[ 13578]|1[02])-(0?[ 1-9]| [12]\d|3[01]) | (((1[6-9]| [2-9]\d) \d{2})-(0?[ 13456789]|1[012])-(0?[ 1-9]| [12]\d|30)] | (((1[6-9]| [2-9]\d] \d{2}) -0?2-(0?[ 1-9]|1\D|2[0-8]) | (((1[6-9]| [2-9]\d] (0[48]|[ 2468][048]| [13579] [26]) | ((16| [2468] [048]| [3579] [26]) 00)) ( -0?2-29-)) $

C # Regular Expressions
Photo src[^>]*[^/]. (?: jpg|bmp|gif) (?:\"| \‘)
Chinese ^ ([\u4e00-\u9fa5]+|[ a-za-z0-9]+) $
URL "\<a.+?href=[" "" "(?! http\:\/\/) (?! Mailto\:) (? >foundanchor>[^ ' "" >]+?) [^>]*?\> "

Regular expressions that match Chinese characters: [\U4E00-\U9FA5]

Match double-byte characters (including kanji): [^\x00-\xff]

Regular expression that matches a blank line: \n[\s|] *\r

Regular expression matching HTML tags:/< (. *) >.*<\/\1>|< (. *) \/>/

Regular expression matching the leading and trailing spaces: (^\s*) | (\s*$) (Trim function like VBScript)

Regular expression matching email address: \w+ ([-+.] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] \w+) *

Regular expression matching URL URL:/http ([\w-]+\.) +[\w-]+ (/[\w-./?%&=]*)?

C # Regular Expressions

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.