C # Learning Note "1"--regex class

Source: Internet
Author: User

C#regex is a regular expression class
Used for string processing to find a matching string.
1, first look at an example
Regex regex=new Regex (@ "OK");//We want to find "OK" in the target string
Match M=regex. Match ("Fjsladfok");
Console.WriteLine (M.tostring ());//This is the find OK
The result is: OK
2,
Regex regex=new Regex (@ "\d+");//\d+ is a digital representation
Match M=regex. Math ("JFLSADKJ 98");
Console.WriteLine (m.tostring);//result is 98
3, the key is how to write special characters
"\w+" represents the letter "\w+" for the character "\s+" means the space "$" for the end of the string
4,

There are several names for zero-width assertions, as well as a look around, or a pre-search, which I use in MSDN, with the following types of zero-width assertions:

(? = subexpression): 0 width Positive lookahead assertion. The match continues only if the subexpression matches the right side of this position. For example, 19 (? =99) matches the 19 instance that precedes 99.

(?! sub-expression): 0 width Negative lookahead assertion. The match continues only if the sub-expression does not match to the right of the position. For example, (?! 99) matches a word that does not end with 99, so it does not match 1999.

(? <= sub-expression): 0 width is being recalled after the assertion is issued. The sub-expression continues to match only if the subexpression matches the left side of this position. For example, (? <=19) 99 matches an instance of 99 followed by 19. This construct does not backtrack.

(? <! sub-expression): 0 width negative review post assertion. The match continues only if the subexpression does not match to the left of this position. For example (? <!19) matches a word that does not start with 19, so it does not match 1999.

eg

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Text.RegularExpressions;namespaceRegular Expression {classprogram{Static voidMain (string[] args) {stringp =@"( ^\[) ([A-z]{1}) (\d+) ([A-z]) (\s+) (? =\])";stringText ="[s434b]JF"; Regex regex=NewRegex (P, regexoptions.ignorecase); MatchCollection MC=regex. Matches (text);foreach(Match mainchMC) { for(intK =0; K < Ma. Groups.count; k++) {Console.WriteLine ("Match groud{0}: {1}", K, MA. GROUPS[K]. Value);}} Console.read (); }}}


The search results are not "", so this means "0 width assertion" is just a condition.

5,

Regular expression meta-characters

The regular expression language consists of two basic character types: literal (normal) text word and metacharacters character. Metacharacters enable regular expressions to have processing power. Metacharacters can be any single character placed in [] (such as [a] to match a single lowercase character a), or a sequence of characters (such as [a-d] that matches any one of the characters between A, B, C, D, and \w for any English letter and number and underscore), following some common metacharacters :

Metacharacters

Description

.

matches any character except \ n (note that the meta-character is a decimal point).

[ABCDE]

Match any one of the characters in the ABCDE

[A-h]

Match any one character between A and H

[^FGH]

does not match any one of the characters in the FGH

\w

Matches the uppercase and lowercase characters and any one or more of the numbers from 0 to 9, equivalent to [a-za-z0-9_]

\w

does not match uppercase and lowercase characters and any one of the numbers 0 to 9, equivalent to [^a-za-z0-9_]

\s

Matches any whitespace character, equivalent to [\f\n\r\t\v]

\s

Matches any non-whitespace character equivalent to [^\s]

\d

Matches any single number between 0 and 9, equivalent to [0-9]

\d

does not match any single number between 0 and 9, equivalent to [^0-9]

[\u4e00-\u9fa5]

Matches any single character (Unicode is used to denote Chinese characters)

Regular Expression Qualifier

The metacharacters above are all matched against a single character, and a qualifier is required to match more than one character at a time. Here are some common qualifiers (both N and M in the following table represent integers, and 0<n<m):

Limited float

Description

*

Match 0 to multiple metacharacters, equivalent to {0,}

?

Matches between 0 and 1 metacharacters, equivalent to {0,1}

N

Match N metacharacters

{N,}

Match at least N metacharacters

{N,m}

Match N to M of metacharacters

+

Match at least 1 metacharacters, equivalent to {1,}

\b

Match word boundaries

^

The string must begin with the specified character

$

The string must end with the specified character

Description

(1) because in the regular expression "\", "?" "," * "," ^ "," $ "," + "," (",") "," | "," {"," ["characters already have a special meaning, if they need to use their original meaning, it should be escaped, for example, you want to have at least one" \ "in the string, then the regular expression should be written: \\+.

(2) Multiple metacharacters or literal text characters can be enclosed in parentheses to form a grouping, for example ^ (4-9]\d{8}$) [indicates any mobile phone number starting with 13.

(3) In addition, for Chinese characters matching is the corresponding Unicode encoding to match, for a single Unicode character, such as \u4e00 represents the Chinese character "one", \u9fa5 represents the Chinese character "calls", in Unicode encoding This is the first and last of the Chinese characters can be represented Unicode encoding, which can represent 20,901 characters in Unicode encoding.

(4) Regarding the use of \b, which represents the beginning or end of a word, with the string "123a 345b 456 789d" As the example string, if the regular expression is "\b\d{3}\b", only 456 can be matched.

(5) You can use the | "To represent or a relationship, such as [Z|j|q], that matches any one of the letters in Z, J, Q.

(Refer to Http://blog.csdn.net/zhoufoxcn/archive/2010/03/12/5372420.aspx for the above content)

C # Learning Note "1"--regex class

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.