Advanced | skills | regular
Because. NET basic regular syntax and PERL5 are basically the same, so the basic syntax you can go to download the m$ JS Help document, there is a detailed description of what \d represents, {, 5}, what \[means ..., here I just want to remind you that in order to avoid conflicts with reverse references, you use \ NN represents octal ASCII code, please add 0 after \, that is, \40 when the ASCII code, 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.
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 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:
$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 once. So, with the inline option, you can define a group in a regex for the size of the case, a group is not the size of the case, is not very convenient?