Replacement class of. NET regular expressions using advanced techniques

Source: Internet
Author: User
Tags regex expression
Because. the basic regular Syntax of. net is basically the same as that of Perl5. Therefore, you can download the M $ JS help document with detailed descriptions on the section \ d ,{, 5} represents what, \ [represents what ......, Here I just want to remind you that, to avoid conflicts with reverse references, add 0 after \ nn to represent the ASCII code of octal, that is, when \ 40 represents the ASCII code, please write \ 040 in this way.

Replace

The Regex class has a static Replace method, and its instance also has a Replace method. This method is very powerful, because it can be passed into a delegate, so that you can customize each capture matching, how to process captured content.

Public static void Main ()
{
String s = "1 12 3 5 ";
S = Regex. Replace (s, @ "\ d +", new MatchEvaluator (CorrectString), RegexOptions. Compiled | RegexOptions. IgnoreCase );
Console. WriteLine (s );
Console. ReadLine ();
}
Private static string CorrectString (Match match)
{
String matchValue = match. Value;
If (matchValue. Length = 1)
MatchValue = "0" + matchValue;
Return matchValue;
}

The above Code shows that if delegate MatchEvaluator is used to process the regular Match result, the Code returns "01 12 03 05 ". In addition to using delegate to process captured matches, the Replace method can also use strings to Replace the Match results, in addition to static replacement of the Match result into a fixed text, you can also use the following syntax to conveniently implement the required functions:

$ Number Replace the matching number group with the replacement expression. It is unclear how to write this sentence. Let's take an example:

Public static void Main ()
{
String s = "1 12 3 5 ";
S = Regex. Replace (s, @ "(\ d + )(? # Note) "," 0 $1 ", RegexOptions. Compiled | RegexOptions. IgnoreCase );
Console. WriteLine (s );
Console. ReadLine ();
}

This code returns "01 012 03 05"

That is to say, each matching result of group 1 is replaced by the expression "0 $1". "$1" in "0 $1" is substituted by the matching result of Group 1.

$ {Name} Replace the matched group named "name" with an expression,

Change the Regex expression in the previous example @"(? <Name> \ d + )(? # This is a comment.) The Replacement Formula after "0 $ {name}" is the same.

$ Run the $ escape character. In the above example, the expression is changed @"(? <Name> \ d + )(? # Note) "and" $ {name} ", the result is" $1 $12 $3 $5"
$ & Replace the entire match
$' Replace the character before matching
$' Replace matching characters
$ + Replace the last matched group
$ _ Replace the entire string

  Let's look at the following options.

* Note (? # This is a comment.) The regular inline comment syntax is (? #)

  Expression Option

RegexOptions provides the following options. For more information, see online help.

RegexOptions enumerated values Inline flag Simple Description
ExplicitCapture N Only groups with names or numbers are captured.
IgnoreCase I Case Insensitive
IgnorePatternWhitespace X Remove non-escape white space in mode and enable annotation marked.
MultiLine M The principle of Multiline mode is to modify the meaning of ^ and $.
SingleLine S Single-line mode, which corresponds to MultiLine

  
The Inline flag I mentioned here is because inline flag can define matching options in a smaller granularity (in groups) than the global option that uses RegexOptions to define a Regex expression in new Regex, this makes it easier to express our thoughts.

The syntax is as follows :(? I: expression) to define an option ,(? -I: expression) is to delete an option ,(? I-s: expression) defines I, deletes s, yes, we can define multiple options at a time. In this way, with the inline option, you can define a group in a Regex as case-sensitive. Is it very convenient for a group to be case-insensitive?

Posted on infinite. net reading (...) Comment (...) EDIT favorites

Refresh comment refresh page Back to Top

Blog homepage blog news flash programmer recruitment Knowledge Base

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.