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

timeout

Starting with.NET Framework 4.5, regular expressions support specifying a timeout during a match operation. If the match timeout, will be thrown RegexMatchTimeoutException.

All methods add overloads 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);

Copy the code

If your application needs to process arbitrary regular expressions (for example, in the advanced search dialog), it’s important to use this parameter to prevent some malicious regular expression from causing infinite computations.

Compiling regular expressions

The RegexOptions.Com Compiled option enables Regex instances to dynamically build and compile code against a particular regular expression with a lightweight code generator, improving matching speed.

Pattern modifier

Mode modifiers can be turned off as well as on. In the following example, case ignoring is enabled and then case ignoring is disabled. Therefore, the matching result is Aa.

Regex.Match("AAAa"."(? i)a(? -i)a").Value;    // Aa
Copy the code

Zero width assertion

Now write a regular expression to verify that the password meets the requirement that it contains at least one number.

This is easy. I’ll just do it like this

Regex.IsMatch("12345678"."\d");
Copy the code

Now let’s add a condition that it’s longer than 6 digits. It seems impossible to implement with a single re.

Yes, you can, using the zero-width forward first assertion.

Forward prior assertion (? =exp), usually used to match the content before exp. For example, in the following example, to get the name, you need to match the previous content.

Regex.Match("Name Zhang SAN, male, 30 years old."."(? < = name). *? (? =)").Value;  / / zhang SAN
Copy the code

In fact, the correct understanding is: forward first assertion, after the match is successful, will be back to the starting position, and then continue the subsequent match.

The most important thing here is that the match goes back to the starting position, so the correct way to think about it is a forward conditional judgment.

The password must contain at least one digit longer than 6:

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

To make it more difficult, the password must meet the following requirements:

  • At least eight
  • Contains at least one number
  • Contains at least one lowercase letter
  • Contains at least one uppercase letter
string pattern = @ "(? =.*\d)(? =.*[a-z])(? =.*[A-Z]).{8,}";
Regex.IsMatch("12345678", pattern);  // false
Regex.IsMatch("1234567a", pattern);  // false
Regex.IsMatch("123456aA", pattern);  // true
Copy the code

Split string

The split string delimiter is not included in the result. To include the delimiter in the result, you can include the expression in a forward condition.

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

grouping

Groups with index n can be referenced in regular expressions using the \n syntax.

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

// pop
// peep
Copy the code

Named capture grouping syntax: (? ‘group name’ expression) or (? < group name > expression)

By reference named grouping syntax: \k’ group name ‘or \k< group name >

Replace and split text

The replacement string can access the original match through $0 as an alternative structure. $1, $2 access any captured grouping. For named groups, you can access them as ${name}.

Add <> to all numbers:

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

MatchEvaluator commissioned

The Replace method has an overload that takes the MatchEvaluator delegate as an argument instead of replacement. This delegate performs once for each match and replaces the value in the original string with its return result.

MatchEvaluator delegate definition:

public delegate string MatchEvaluator(Match match);
Copy the code

Example:

Console.WriteLine(Regex.Replace("1 + 11 = 12".@"\d+", m => (int.Parse(m.Value) * 10).ToString()));

// 10 + 110 = 120
Copy the code