[C #] Regular Advanced
.NET in the regular expression is based on regular expressions in Perl 5.
time out
Starting .NET Framework 4.5, support for regular expression matching the specified timeout period in operation. If a match is timed out, it will be thrown RegexMatchTimeoutException
.
All methods are overloaded with increased timeout parameters:
public static Match Match(string input, string pattern, RegexOptions options, TimeSpan matchTimeout); public static MatchCollection Matches(string input, string pattern, RegexOptions options, TimeSpan matchTimeout); public static string Replace(string input, string pattern, string replacement, RegexOptions options, TimeSpan matchTimeout); public static string[] Split(string input, string pattern, RegexOptions options, TimeSpan matchTimeout);
If your application needs to handle any regular expression (for example, in the Advanced Search dialog box) be sure to use this parameter to prevent malicious regular expression leads to infinite computing.
Compiling a regular expression
RegexOptions.Compiled
Option will make Regex
examples of lightweight build and compile code generator dynamically regular expression code for a particular improve matching speed.
Pattern Modifiers
Pattern Modifiers can not only open, but also closed. The following example, to ignore case open, then close ignoring case, the matching result Aa
.
Regex.Match("AAAa", "(?i)a(?-i)a").Value; // Aa
Zero-width assertion
Now write a password used to verify compliance with the requirements of regular expressions, it requires that contains at least one number.
This is very simple, as it
Regex.IsMatch("12345678", "\d");
Now add a condition, the length is greater than six. It seems to use a regular can not be achieved.
In fact, it is possible, with a zero-width assertion forward first assertion it.
Forward first assertion (?=exp)
, generally used to match content before exp. For example the following example, to remove the name, need to match the ,
previous content.
Regex.Match("姓名张三,男,30 岁", "(?<=姓名).*?(?=,)").Value; // 张三
In fact, the correct understanding is: positive first assertion, after the match is successful, will be returned to the starting position, then after the match to continue.
The most important point here is that after the successful return match starting position, so that its understanding is correct, a former judge to the conditions.
Then the above code comprises at least one number, and the like to achieve a length greater than 6:
Regex.IsMatch("abcde6", @"(?=.*\d).{6,}");
We add a little more difficult, password requirements meet the following conditions:
- At least 8
- Contain at least one number
- Comprising at least one lowercase letter
- Contain at least one capital letter
string pattern = @"(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}";
Regex.IsMatch("12345678", pattern); // false
Regex.IsMatch("1234567a", pattern); // false Regex.IsMatch("123456aA", pattern); // true
Split string
Dividing string delimiter is not included in the results, to the separator included in the results, it may be included in the expression before the forward condition.
foreach (string s in Regex.Split("oneTwoThree", "(?=[A-Z])")) Console.WriteLine(s); // one // Two // Three
Packet
Regular expressions can \ n syntax reference index for the group of n.
var m = Regex.Matches("pop pope peep", @"\b(\w)\w+\1\b");
// pop
// peep
Named capturing group syntax: (?'组名'表达式)
or (?<组名>表达式)
Reference a named group syntax: \k'组名'
or \k<组名>
Replace text and segmentation
Replacement string through $0
access to the original match as an alternative structure. $1
, $2
Access any captured packets. For a named group, by ${name}
way of a visit.
To all the numbers add <>:
Console.WriteLine(Regex.Replace("1 + 11 = 12", @"\d+", @"<$0>"));
// <1> + <11> = <12>
MatchEvaluator commission
Replace
The method has an overload, using MatchEvaluator
delegate as a parameter instead replacement
. The commission will be executed once for each match, and returns the result thereof using the replacement value in the original string.
MatchEvaluator
The delegate definition:
public delegate string MatchEvaluator(Match match);
Example:
Console.WriteLine(Regex.Replace("1 + 11 = 12", @"\d+", m => (int.Parse(m.Value) * 10).ToString()));
// 10 + 110 = 120