C#Regular Advancement

Keywords: C#

Regular expressions in.NET are based on Perl 5 regular expressions.

overtime

Beginning with.NET Framework 4.5, regular expressions support specifying timeout times in matching operations.If the match timed out, a RegexMatchTimeoutException would be thrown.

All methods add overload with 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, such as in the Advanced Search dialog box, be sure to use this parameter to prevent some malicious regular expressions from causing infinite calculations.

Compile regular expressions

The RegexOptions.Compiled option will enable Regex instances to dynamically build and compile code for a specific regular expression through a lightweight code generator to improve matching speed.

Pattern modifier

Mode modifiers can be turned on and off.In the following example, ignoring case is turned on, and ignoring case is turned off, so the matching result is Aa.

Regex.Match("AAAa", "(?i)a(?-i)a").Value;    // Aa

Zero Width Assertion

Now write a regular expression to verify that your password meets the requirements, which requires at least one number.

This is very simple, just like this

Regex.IsMatch("12345678", "\d");

Now add a condition that is longer than 6 bits.It seems impossible to do this with a rule.

In fact, it is possible to use the forward-first assertion in the zero-width assertion.

Forward-first assertions (?=exp) are generally used to match what precedes exp.For example, in the following example, to take out a name, you need to match the previous content.

Regex.Match("Name Zhang San, male, 30 years old", "(?<=Full name).*?(?=,)").Value;  // Zhang San

In fact, the correct understanding is that a forward-first assertion that once a match succeeds, it falls back to its starting position and continues to match later.

The most important thing here is that when a match succeeds, it falls back to its starting position, so the correct understanding of it is a forward conditional judgment.

Then the above password contains at least one number and is longer than 6.

Regex.IsMatch("abcde6", @"(?=.*\d).{6,}");

Let's make it a little harder. The password requirements are as follows:

  • At least 8 bits
  • Include at least one number
  • Contains at least one lowercase letter
  • Include 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

The split string separator will not be included in the result. To include the separator in the result, you can include the expression in the forward condition.

foreach (string s in Regex.Split("oneTwoThree", "(?=[A-Z])"))
    Console.WriteLine(s);
    
// one
// Two
// Three

Grouping

Grouping with an index of N can be referenced in regular expressions through the \n syntax.

var m = Regex.Matches("pop pope peep", @"\b(\w)\w+\1\b");

// pop
// peep

Named capture grouping syntax:
(?'Group Name'expression) or (?<Group Name>Expression)

Reference named grouping syntax:
\k'group name' or \k<group name>

Replace and split text

The substitution string can access the original match through $0 as the substitution structure.$1, $2 access any captured grouping.Named groups can be accessed by ${name}.

Add <> to all numbers:

Console.WriteLine(Regex.Replace("1 + 11 = 12", @"\d+", @"<$0>"));
// <1> + <11> = <12>

MatchEvaluator delegation

The Replace method has an overload that uses the MatchEvaluator delegate as a parameter instead of replacement.The delegate will execute once for each match and replace the value in the original string with its return result.

MatchEvaluator 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

Posted by kotun on Tue, 11 Feb 2020 21:11:42 -0800