. NetAdvanced techniques for using regular expressions in
Preface
I. This series of articles does not describe the basic regular syntax.JSYou can find it in the help document.GoogleClick
Ii. Reasons for writing a series of articles
1Regular Expressions are useful and often used
2Some advanced regular expressions have not been understood and mastered by a considerable number of people.
3I just saw an article on the Internet using the regular expression, which gave me the impulse to write this article.
4Most of the articles in this series can be applied to both. NetLanguage,JavascriptAnd so on
Iii. Features of this series of articles: Try to use small examples to illustrate the relatively difficult regular expression syntax that many regular expression books have not clearly stated
4. Content of this series of articles: Advanced syntax for replacement, inline expression options, group, reverse reference, positive declaration, negative declaration, positive and negative declaration, negative anti-declaration, non-backtracing matching, implicit,. NetFeatures of the Regular Expression Engine
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)
{
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?
For more highlights, refer to the next breakdown,
Next article: advanced techniques for using regular expressions in. Net (2)
Advanced techniques for using regular expressions in. Net (3)
Advanced techniques for using regular expressions in. Net (4)