Regular Expression, RegEx class

Source: Internet
Author: User

C # RegEx is a regular expression class
It is used to process strings and find matched strings.
1. Let's look at an example.
RegEx = new RegEx (@ "OK"); // you can find "OK" in the target string"
Match m = RegEx. Match ("fjsladfok ");
Console. writeline (M. tostring (); // This is to find OK
// The result is OK.
2,
RegEx = new RegEx (@ "\ D +"); // \ D + represents a number
Match m = RegEx. Math ("jflsadkj 98 ");
Console. writeline (M. tostring); // The result is 98.
3. The key is how to write special characters.
"\ W +" represents the letter "\ W +" represents the character "\ s +" represents the space "$" represents the end of the string
4,

There are many claims for zero-width assertion, including loop view and pre-search. Here I use the call method in msdn. There are several claims for zero-width assertion:

(? = Subexpression): Zero-width positive prediction predicate. The child expression continues matching only when it matches the right side of the position. For example, 19 (? = 99) matches the 19 instances that are earlier than 99.

(?! Sub-expression): Zero-width negative prediction first asserted. The child expression continues matching only when it does not match the right side of the position. For example ,(?! 99) It does not match the word that does not end with 99. Therefore, it does not match 1999.

(? <= Sub-expression): assertion after reviewing with zero width. The child expression continues matching only when it matches the left side of the position. For example ,(? <= 19) 99 matches the 99 instance following 19. This construction will not be traced back.

(? <! Sub-expression): assertion after review. The child expression continues matching only when it does not match on the left side of the position. For example (? <! 19) It does not match a word that does not start with 19, so it does not match 1999.

Eg:

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. Text. regularexpressions;

Namespace Regular Expression
{
Class Program
{
Static void main (string [] ARGs)
{
String P = @ "(^ \ [) ([A-Z] {1}) (\ D +) ([A-Z]) (\ s + )(? = \]) ";
String text = "[s434b] JF ";
RegEx = new RegEx (p, regexoptions. ignorecase );
Matchcollection MC = RegEx. Matches (text );
Foreach (match Ma In MC)
{
For (int K = 0; k <Ma. Groups. Count; k ++)
{
Console. writeline ("Match groud {0 }:{ 1}", K, Ma. Groups [K]. value );
}
}
Console. Read ();

}
}
}
The search result is not "]", so it indicates that "zero-width assertion" is only a condition.

5,

Regular Expression metacharacters

The Regular Expression Language consists of two basic character types: literal (normal) text characters and metacharacters. Metacharacters enable the regular expression to process. Metacharacters can be any single character in [] (for example, [a] indicates matching a single lowercase character ), it can also be a character sequence (for example, [a-d] indicates matching any character between A, B, C, and D, while \ W indicates any English letter, number, and underline ), below are some common metacharacters:

Metacharacters

Description

.

Match any character except \ n (note that the metacharacter is a decimal point ).

[ABCDE]

Match any character in ABCDE

[A-H]

Match any character between A and H

[^ Fgh]

Does not match any character in fgh

\ W

Match any one of the upper and lower case English characters and numbers 0 to 9 and underline, equivalent to [a-zA-Z0-9 _]

\ W

Does not match any one of the upper and lower case English characters and numbers 0 to 9, equivalent to [^ a-zA-Z0-9 _]

\ S

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

\ S

Matches any non-blank characters, which is equivalent to [^ \ s]

\ D

Matches a single number between 0 and 9, which is equivalent to [0-9].

\ D

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

[\ U4e00-\ u9fa5]

Match any single Chinese character (here unicode encoding is used to represent Chinese characters)

Regular Expression qualifier

The above metacharacters are matched for a single character. To match multiple characters at the same time, you also need to use a qualifier. Below are some common delimiters (N and m in the following table both represent integers and 0 <n <m ):

Floating limit

Description

*

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

?

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

{N}

Match n metacharacters

{N ,}

Match at least N metacharacters

{N, m}

Match n to M metacharacters

+

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

\ B

Match word boundary

^

The string must start with a specified character.

$

The string must end with a specified character.

Note:

(1) because the regular expressions include "\" and "? "," * "," ^ "," $ "," + "," (",") "," | "," {"," [", And other characters have some special significance. If you need to use their original meanings, escape them, for example, if you want to have at least one "\" in the string, the regular expression should be written as follows: \ +.

(2) You can enclose multiple metacharacters or literal text characters in parentheses to form a group, such as ^ (13) [4-9] \ D {8} $ indicates any mobile phone number starting with 13.

(3) In addition, Chinese characters are matched using the corresponding unicode encoding. For a single UNICODE character, for example, \ u4e00 indicates the Chinese character "1 ", \ u9fa5 indicates the Chinese character "second". In unicode encoding, this is the first and last unicode encoding of the Chinese characters that can be expressed. In unicode encoding, this can represent 20901 Chinese characters.

(4) For the usage of \ B, it indicates the start or end of a word. The string "123a 345b 456 789d" is used as the sample string, if the regular expression is "\ B \ D {3} \ B", it can only match 456.

(5) "|" can be used to represent or. For example, [z | j | q] indicates matching any letter in Z, J, and Q.

(The above content reference http://blog.csdn.net/zhoufoxcn/archive/2010/03/12/5372420.aspx)

Regular Expression, 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.