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

Net regular classes and the application of their methods

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

Share

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

This article shows you how .NET regular classes and their methods are applied. The content is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

1 Overview

When learning regularization, I am not familiar with Regex class, and I don't know which method to solve the problem. Combined with some typical application scenarios of regular application, this paper introduces the basic application of Regex class. Here, we focus on the introduction of .NET class, and do not discuss the application of regularization in depth.

In the end, the application of regularization is pattern matching, and according to the different purposes, it can be divided into the following applications: verification, extraction, replacement, segmentation. Combined with the controls, classes and class methods provided by .NET, these applications can be easily realized.

The following will be combined with some typical application scenarios to introduce the common classes, methods and properties in .NET. This article aims to guide the basic usage of .NET classes, and does not discuss the regular expressions involved in it in depth. This article is suitable for beginners who use rules on the .NET platform.

2 basic application

2.1 Verification

The purpose of verification is to determine whether the input source string conforms to a certain rule or rule. depending on the requirements, it may be to verify the whole source string or just one of the substrings.

There are generally two applications of validation in .NET, one is in the validation control RegularExpressionValidator, and the other is in the program.

2.1.1 validation Control RegularExpressionValidator

RegularExpressionValidator is a client-side verification control that comes with .NET. Through simple settings, you can complete the verification of the input value of a control.

The basic application syntax is as follows:

I don't say too much about the RegularExpressionValidator control, but just a few points to pay attention to when using it:

1. RegularExpressionValidator performs client-side verification.

2. JavaScript grammar rules are used regularly in RegularExpressionValidator.

3. The RegularExpressionValidator control cannot verify that the input is empty.

Because RegularExpressionValidator does client-side authentication, it is easy to skip, so server-side authentication is also done while using RegularExpressionValidator authentication.

RegularExpressionValidator ultimately generates client-side JavaScript code for verification, so the rules used by RegularExpressionValidator are required to comply with JavaScript syntax rules, which are different from .NET:

1. Look around in reverse order is not supported, that is, metacharacters only support ASCII codes, that is,\ w is equivalent to [a-zA-Z0-9] and\ d is equivalent to [0-9].

RegularExpressionValidator controls are generally used to verify whether the string entered by a control conforms to a certain rule as a whole, so "^" and "$" are usually necessary; when using the relationship "|" represents "or", be sure to use "()" to limit the scope of "|", for example, 0-100 can write "^ ([1-9]? [0-9] | 100) $".

RegularExpressionValidator cannot verify that the input is empty and that you want to use the RequiredFieldValidator control.

RegularExpressionValidator verification control is one of a group of verification controls encapsulated by .NET to facilitate client verification, but because RegularExpressionValidator is limited by supporting regular syntax rules, it can only do limited format checking, and some complex checks can be realized by writing JavaScript code, which is also very simple.

2.1.2 Program verification-IsMatch ()

The verification in the program basically uses the IsMatch method, and the object of verification may be the whole of the source string or just one of the substrings.

Verifying that the overall source string conforms to a certain rule is basically consistent with the requirements when using RegularExpressionValidator, but because you are in a .NET program, you use the .NET syntax, which is much more powerful than in JavaScript. For example, verifying whether the string entered in a text box conforms to a certain rule is a typical requirement for verification as a whole.

Example 1: verify the textBox1 input, the integer part is required to be 0 or positive integer, the decimal is optional, and the decimal must be 2 digits.

Regex reg = new Regex (@ "^ (?: [1-9] [0-9] * | 0) (?:\. [0-9] {2})? $"); if (reg.IsMatch (textBox1.Text)) {richTextBox2.Text = "input format is correct!" ;} else {richTextBox2.Text = "input format error!" ;}

Because the source string as a whole is validated, "^" and "$" are essential. Otherwise, the result of verification may be wrong, such as the regular expression "(?: [1-9] [0-9] * | 0) (?:. [0-9] {2})?", when entering "0.123", the matching result is "0.12". At this time, the regular only plays the role of matching, not the role of verification.

Verifying whether the part of the source string conforms to a certain rule is the verification of the substring in the source string, which is usually used to determine whether the source string contains or does not contain substrings that conform to a certain rule, which is similar to IndexOf in the string class.

Example 2 (refer to two regular expressions):

Data:

1985aaa1985bb

Bcae1958fiefadf1955fef

Atijc1944cvkd

Df2564isdjfef2564d

Abc1234def5678ghi5678jkl

Requirement 1: verify whether the four consecutive digits that appear anywhere in the string are duplicated in the whole string. The repetition is True and the non-repetition is False.

The verification result of the above data requirement 1 is True:

1985aaa1985bb

Df2564isdjfef2564d

Abc1234def5678ghi5678jkl

Because there are duplicates in 4 consecutive digits indicated in the requirement at any location, it is verified when traversing each location in the source string before finding the duplicate, so the starting identifier "^" cannot be qualified. In the matching process, unless there is no repetition to the end, all you have to do is match to the repeating position, so there is no need for the closing identifier "$", so this is a typical requirement for subserial verification of strings.

Code implementation:

String [] test = new string [] {"1985aaa1985bb", "bcae1958fiefadf1955fef", "atijc1944cvkd", "df2564isdjfef2564d", "abc1234def5678ghi5678jkl"}; Regex reg = new Regex (@ "(\ d {4})) (?: (?!\ 1).) *\ 1"); foreach (string s in test) {richTextBox2.Text + = "Source string:" + s.PadRight (25,') + "Verification result:" + reg.IsMatch (s) + "\ n" } / *-output-source string: 1985aaa1985bb verification result: True source string: bcae1958fiefadf1955fef verification result: False source string: atijc1944cvkd verification result: False source string: df2564isdjfef2564d verification result: True source string: abc1234def5678ghi5678jkl verification result: True*/

Since the problem of repetition is involved, reverse references are used here. For details of reverse references, you can refer to the regular basis-reverse references.

Requirement 2: verify whether there are duplicates in the first four consecutive digits in the string. The repetition is True, and the non-repetition is False.

The verification result of the above data requirement 2 is True:

1985aaa1985bb

Df2564isdjfef2564d

Because the requirement indicates whether there is a duplicate or not, there needs to be a start identifier "^" to ensure that it is the first consecutive four digits In the matching process, unless there is no repetition to the end, as long as the match is in a repeating position, there is no need for the ending identifier "$", so this is still a requirement for subserial verification of the string, but a qualification is added to requirement 1.

Code implementation:

String [] test = new string [] {"1985aaa1985bb", "bcae1958fiefadf1955fef", "atijc1944cvkd", "df2564isdjfef2564d", "abc1234def5678ghi5678jkl"}; Regex reg = new Regex (@ "^ (?: (?!\ d {4}).) * (\ d {4}) (?: (?!\ 1)) *\ 1") Foreach (string s in test) {richTextBox2.Text + = "Source string:" + s.PadRight (25,') + "Verification result:" + reg.IsMatch (s) + "\ n" } / *-output-source string: 1985aaa1985bb verification result: True source string: bcae1958fiefadf1955fef verification result: False source string: atijc1944cvkd verification result: False source string: df2564isdjfef2564d verification result: True source string: abc1234def5678ghi5678jkl verification result: False * /

2.2 extraction-Match (), Matches ()

The main purpose of extraction is to obtain one or more substrings that conform to a certain rule or rule from the source string. Generally speaking, extraction is widely used in string processing. Match () and Matches () methods are often used in extraction, as well as some methods of Match class and MatchCollection class in result processing, and sometimes some methods of Capture class are also used.

2.2.1 extract single match content-Match ()

You can use the Match () method when there is only one content to extract, or when you only need to get the content that matches successfully for the first time. When using the Match () method, as long as the match succeeds at a certain location, it stops trying to match and returns an object of type Match.

For example: name extraction

Source string: name: Zhang San, gender: male, age: 25

Code implementation:

String test = "name: Zhang San, gender: male, age: 25"; Regex reg = new Regex (@ "(?] *)\ 1 [^ >] * > (?

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