Advanced functions of ASP. NET Regular Expressions
Regular expressions have two functions that you have to say: one is "Naming group" and the other is "lookaround processing ). These functions are rarely used.
Naming group for ASP. NET Regular Expressions
Using a naming group, you can name the matching group separately, and then reference these groups in the expression using the program language. This function is particularly effective if you use the Replace method to reset the format of the input string by re-arranging the order and replacing the elements in the input string. For example, assume that the date uses a string in MM/DD/YYYY format, and you want the date format to be DD-MM-YYYY. At this time, you can write an expression to capture the first format, traverse its matching set, analyze each string, and then use the string operation to create a replacement string. This requires a lot of code and processing. If you use a naming group, you can complete the same task. For details, see:
- String MDYToDMY(String input)
- {
- return Regex.Replace(intput, @"\b(?<month>\d{1,2})/(?<day>\d{1,2}/(?<year>\d{4})\b", "${day}-
- ${month}-${year}");
- }
You can also reference a group by number or by name. In any case, such a reference is called "reverse reference ". Another scenario where reverse references are often used is to match the expression itself. The following expression is used to find duplicate letters:[A-z] \ 1. It will match "aa", "bb", and "cc", but it is different from[A-z] {2}Or[A-z] [a-z]The latter two are equivalent, and the latter two can match "AB" or "ac" or any other combination of two letters. Reverse reference allows the expression to remember some characters in the input string that have been analyzed and matched by the expression.
"Four-way processing" of ASP. NET Regular Expressions"
"Four-way processing" refers to the positive and negative Lookahead and Lookbehind functions supported by many regular expression engines. Not all regular expression engines support four-way authentication. These constructs do not use characters, even if they can match characters. Some modes may not be described without four-way processing. In particular, when a part of the pattern is dependent on another part, such a pattern cannot be described without four-way processing. The following describes the syntax of each four-way processing.
Syntax |
Description |
(? = ...) |
Positive Lookahead |
(?!...) |
Negative Lookahead |
(? <= ...) |
Positive Lookbehind |
(? <!...) |
Negative Lookbehind |
Password verification is an example that requires four steps. It is assumed that the password must be 4 to 8 characters long and contain at least one number. To do this, you can only test in matching\ DAnd then use the string operation to test the length. To implement this in a regular expression, you must use Lookahead. In particular, the regular lookahead is shown as follows:^ (? =. * \ D). {4, 8} $
- Basic learning materials for. NET regular expressions
- What is a regular expression: its historical relationship with the. NET Framework
- C # Regular Expressions
- Use regular expressions to make C # determine whether the input date format is correct
- Use regular expressions in. NET in four cases