. NET Regular expressions use advanced techniques to replace class introduction _ Regular Expressions

Source: Internet
Author: User
Tags readline regex expression
What does \d mean, {, 5} represents what, what \[means ..., here I just want to remind you that in order to avoid conflicts with the reverse reference, when you use \NN to represent the octal ASCII code, please add 0, that is to say, \40 when the ASCII code is expressed, please write \040.

Replace

The Regex class has a static replace method, and its instance has a replace method, which is powerful because it can pass in a delegate so that you can customize how the captured content is handled each time the capture matches.
The above code shows that if you use delegate matchevaluator to handle a regular match result, the code returns "01 12 03 05". Instead of using delegate to handle captured match, the Replace method can replace the match result with a string, replacing the match result with a string in addition to replacing the match result statically with a fixed text, You can also use the following syntax to make it easier to implement the features you need:
Copy Code code as follows:

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;
}


$number

Replace the matching number group with the replacement expression, and how to write this sentence is not clear meaning, or to an example:

public static void Main ()
{
string s = "1 12 3 5";
s = Regex.Replace (s,@ "(\d+) (? #这个是注释)", "0$1", regexoptions.compiled|regexoptions.ignorecase);
Console.WriteLine (s);
Console.ReadLine ();
}

This code returns "01 012 03 05".

That is, each match result for Group One is replaced with the expression "0$1", "$" in "0$1" is substituted by the result of the group 1

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

The previous example of the regex expression changed to "0${name}" after the replacement with the @ "(? <name>\d+) (? #这个是注释)" Result is the same
$$ The escape character to do $, as in the previous example expression to @ "(? <name>\d+) (? #这个是注释)" and "$$${name}", the result is $ $ $
$& Replace the entire match
$` Replace the character before the match
$' Replace a matching character
$+ Replace the last matched group
$_ Replace the entire string
  
After the options, you can write an example to savor.

* Note, the (? #这个是注释) In the example above illustrates that the regular inline annotation syntax is (? #).

   Expression Item Options

The regular expression option RegexOptions has the following options, for more information please refer to the online Help

RegexOptions enumeration value Inline flag Simple description
Explicitcapture N A named or numbered group is captured only if it is defined
IgnoreCase I Case insensitive
Ignorepatternwhitespace X Eliminates non-escaped whitespace in the schema and enables annotations marked by #.
MultiLine M Multiline mode, whose principle is to modify the meaning of ^ and $
Singleline S Single-line mode, and multiline corresponds
  
Here I refer to the inline flag because the inline flag can be more granular (group-per-unit) definition matching options than the global option to define a regex expression with regexoptions in new regex, making it easier to express our thoughts

The

syntax is this: (? i:expression) to define an option, (?-i:expression) to delete an option, (? i-s:expression) to define I, Delete S, yes, we can define many options at a time. So, with the inline option, you can define a group in a regex that is case-by-case, isn't it convenient?

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.