Three matching modes of Regular Expressions

Source: Internet
Author: User
Tags expression engine

The first time I wrote this article, I am not concise. Please kindly advise me! Thank you!

Regular Expressions are common tools used to process strings. In C #, we generally use the RegEx class to represent a regular expression. The Regular Expression Engine supports the following three matching modes: singleline, multiline, and ignorecase ).

1. singleline)

Msdn definition: change the meaning of a vertex (.) so that it matches each character (instead of each character except \ n ).

A typical scenario of using the single-line mode is to obtain information in the webpage source code.

Example:

We use the webbrowser control and obtain the HTML source code from http://www.xxx.com/1.htm. the memory is stored in the variable string:

Html
< Html >
< Body >
< Div >
Line 1
Line 2
</ Div >
</ Body >
</ Html >

We want to extract the DIV tag and its content and writeCodeAs follows:


String Pattern =   @" <Div>. * </div> " ;
RegEx =   New RegEx (pattern );
If (RegEx. ismatch (STR ))
Console. writeline (RegEx. Match (STR). value );
Else
Console. writeline ( " Mismatch! " );

// Result: mismatch!

Error analysis:

It is generally considered that the dot sign (.) matches any single character, and (. *) matches any number of characters. But in fact, the dot symbol cannot match the line break. In Windows, the equivalent expression is [^ \ r \ n].

The HTML source code we obtain from the website rarely contains line breaks. At this time, the single-line mode comes in handy. It can change the meaning of the dot symbol. Modify the RegEx constructor and use regexoptions. singleline to declare the single row mode:


String Pattern =   @" <Div>. * </div> " ;
RegEx =   New RegEx (pattern, regexoptions. singleline );
If (RegEx. ismatch (STR ))
Console. writeline (RegEx. Match (STR). value );
Else
Console. writeline ( " Mismatch! " );

/**/ /*
Result:
<Div>
Line 1
Line 2
</Div>
*/

Single Row mode embedding modifier:

We can embed the single-row mode directly in the regular expression:

(? S) <div>. * </div>

(? S) modifier description. The expression following it adopts the single line mode. Therefore, do not put it at the end. You can also use (? -S.

Note: the priority of the embedded mode is higher than the regexoptions setting of the RegEx class, so (? S), whether or not regexoptions. singleline is used, it is parsed in single row mode.

2. multiline)

Msdn definition: change the meaning of ^ and $ so that they match the beginning and end of any row, not just the beginning and end of the entire string.

Example:

Each line of a text file is a user name that reads the file into the variable STR for processing. The content is as follows:

Twenty-four painters
Terrylee
Moji
Dflying Chen
Rainy

I borrowed the name of my predecessors at the blog site :)

We want to find a username that starts with an English letter. The Code is as follows:


String Pattern =   @" ^ [A-Za-Z] + .* " ;
RegEx =   New RegEx (pattern );
If (RegEx. ismatch (STR ))
Console. writeline (RegEx. Match (STR). value );
Else
Console. writeline ( " Mismatch! " );

// Result: mismatch!

Error analysis:

(^) Is the starting position of the string. the first character of STR is a Chinese character, so it cannot match. We can use the multiline mode to change the meaning of (^) so that it matches the start of each row, rather than the start of the entire string.

The change code is as follows:


String Pattern =   @" ^ [A-Za-Z] + .* " ;
RegEx =   New RegEx (pattern, regexoptions. multiline );
If (RegEx. ismatch (STR ))
Console. writeline (RegEx. Match (STR). value );
Else
Console. writeline ( " Mismatch! " );

// Result: terrylee

At the same time, the multiline mode also changes the meaning of ($) so that it matches the end of each row, rather than the end of the entire string.

Unlike (^) and ($), (\ A) and (\ Z) are not affected by the multiline mode and always match the start and end of the entire string.

Multiline mode embedding modifier:(? M) and (? -M)

3. Ignore case sensitivity (ignorecase)

Msdn definition: Specifies case-insensitive matching.

This mode is easy to understand. It considers the uppercase and lowercase characters to be the same. We will continue to illustrate the above examples.

Example:


String Pattern =   @" ^ [A-Z] + .* " ;
RegEx =   New RegEx (pattern, regexoptions. multiline | Regexoptions. ignorecase );
If (RegEx. ismatch (STR ))
Console. writeline (RegEx. Match (STR). value );
Else
Console. writeline ( " Mismatch! " );

// Result: terrylee

Analysis: note that the regular expression used this time does not contain uppercase letters, but matches the name starting with an uppercase letter. This is case-insensitive.

Case-insensitive embedding modifier:(? I) and (? -I)

Summary:

Finally, let's use a table to summarize the three models.

Definition Affected expression Regexoptions Enumeration Embedded identifier
Single Row Mode Change the meaning of a vertex (.) so that it matches each character (instead of each character except \ n ). . Singleline (? S)
Multiline Mode Change the meaning of ^ and $ so that they match the beginning and end of a row, not just the beginning and end of the entire string. ^ $ Multiline (? M)
Case Insensitive Specifies case-insensitive matching. Ignorecase (? I)

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.