because. NET basic regular syntax and PERL5 are basically the same, so the basic syntax you can download the m$ JS Help document, which has detailed instructions \d what to say, {,5} What does \[mean ..., 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 after \, that is, \40 in the expression of ASCII code, please write this040. Replacing the Regex class has a static replace method, and its instance also 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 a match is captured. The above code shows that if you use delegate matchevaluator to handle a regular match result, the code returns" the". The Replace method, in addition to using delegate to handle the captured match, can also replace the match result with a string and replace 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 functionality you need: Copy the code code as follows: Public Static voidMain () {strings ="1 3 5"; s= Regex.Replace (S,@"\d+",NewMatchEvaluator (correctstring), regexoptions.compiled|regexoptions.ignorecase); Console.WriteLine (s); Console.ReadLine (); } Private Static stringcorrectstring (match match) {stringMatchvalue =match. Value; if(Matchvalue.length = =1) Matchvalue="0"+Matchvalue;returnMatchvalue;} $number Replace the matching number group with a substitution expression, and the sentence does not express the meaning of how it is written, or an example: Public Static voidMain () {strings ="1 3 5"; S= Regex.Replace (S,@"(\d+) (? #这个是注释)","0$1", regexoptions.compiled|regexoptions.ignorecase); Console.WriteLine (s); Console.ReadLine (); This code returns the " on 012 Geneva to"That is, each matching result for Group One is used"0$1"This expression to replace,"0$1"In" $"The result of matching group 1 is ${name} put the matching group name"name"is replaced with an expression, the regex expression in the above example is changed to@"(? <name>\d+) (? #这个是注释)"The later substitution is changed to"0${name}"The result is the same as the $$ to do the escape character of $, as in the above example the expression is changed to@"(? <name>\d+) (? #这个是注释)"And"$$${name}", the result is" $ $ $" $&replace entire match $ ' replace the character before match $'replace the character after the match$+replace the last matched group $_ replace the option after the entire string, and let's write an example of how to smell it. * Note that the (? #这个是注释) In the example above illustrates the regular inline annotation syntax for (?#) Expression options Regular expression option RegexOptions has the following option, please refer to the online Help RegexOptions enumeration value inline flag for a simple description explicitcapture n only the named or numbered group is defined to capture Ignore Case I does not differentiate between case I ignorepatternwhitespace x de-escaping whitespace in the pattern and enable annotations marked by #. MultiLine m Multi-line mode, the principle is to modify the^and the meaning of Singleline s single-line mode, and multiline corresponds here I refer to the inline flag because, as opposed to the global option to define a regex expression with regexoptions at new regex, Inline flags can define matching options in smaller granularity (in groups), making it easier to express our thought syntax like this: (I:expression) to define an option, (?-i:expression) to delete an option, (? i-s:expression) define I, delete s, yes, we can define a number of options at once. So, with the inline option, you can define a group in a regex to be of the same size, and a group that is not case-sensitive, is it convenient?
Introduction to replacing classes of. NET Regular expressions using advanced techniques