Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to advance C # regular expressions

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

How to carry out C# regular expression upgrade, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

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

Timeout

Starting with .NET Framework 4.5, regular expressions allow you to specify a timeout in a matching operation. If the match times out, a RegexMatchTimeoutException is thrown.

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)

If your application needs to handle arbitrary regular expressions (such as in the advanced search dialog box), be sure to use this parameter to prevent some malicious regular expressions from causing infinite evaluation.

Compile regular expressions

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

Pattern modifier

Mode modifiers 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.

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

Zero width assertion

Now you want to write a regular expression that verifies that the password meets the requirements, which is required to contain at least one number.

This is very simple. The following will do.

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

Now add a condition that the length should be greater than 6 bits. It seems impossible to achieve it with a rule.

In fact, it is possible to use the positive antecedent assertion in the zero-width assertion.

Positive antecedent assertions (? = exp) are generally used to match content before exp. For example, in the following example, to take out the name, you need to match the previous content.

Regex.Match ("name Zhang San, male, 30 years old", "(?

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report