SoFunction
Updated on 2025-03-07

C# regular expressions advanced

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

time out

Starting with .NET Framework 4.5, regular expressions support specifying timeouts in match operations. If the match timeout, a RegexMatchTimeoutException will 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 the application needs to handle arbitrary regular expressions (for example 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

Options will enable Regex instances to dynamically build and compile code for specific regular expressions through a lightweight code generator, improving matching speed.

Pattern correction symbol

The mode modifier can not only be turned on but also closed. In the following example, first turn on ignore case and then turn off ignore case, so the matching result is Aa.

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

Zero width assertion

Now you want to write a regular expression to verify that the password meets the requirements, requiring at least one number.

This is very simple, just as follows

("12345678", "\d");

Now add a condition, the length must be greater than 6 digits. It seems that it cannot be achieved with a regular.

In fact, it is OK. Just use the forward assertion in the zero-width assertion to be positive.

Forward assertion (?=exp), which is generally used to match the content before exp. For example, in the following example, to take out the name, you need to match the previous content.

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

In fact, the correct understanding is: forward assert first, after the match is successful, it will return to the starting position and then continue the subsequent match.

The most important point here is that after the match is successful, the return to the starting position, so the correct understanding of it is a forward condition judgment.

Then the above password contains at least one number and the length is greater than 6, so it is achieved:

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

Let's add a little more difficulty, and the password requirements meet the following conditions:

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

Split string

The split string separator is not included in the result, and to include the delimiter in the result, you can include the expression in the forward condition.

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

Grouping

In regular expressions, groups with index n can be referenced through the \n syntax.

var m = ("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 replacement string can access the original match via $0 as the alternative structure. $1, $2 Access any captured grouping. For named groups, they can be accessed through ${name}.

Add <> to all numbers:

(("1 + 11 = 12", @"\d+", @"<$0>"));
// <1> + <11> = <12>

MatchEvaluator Delegation

The Replace method has an overload, using the MatchEvaluator delegate as a parameter, instead of replacement. The delegate will be executed once for each match and replaces the value in the original string with its return result.

MatchEvaluator Delegation Definition:

public delegate string MatchEvaluator(Match match);

Example:

(("1 + 11 = 12", @"\d+", m => (() * 10).ToString()));

// 10 + 110 = 120

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.