Naming group and four-way processing for ASP. NET Regular Expressions

Source: Internet
Author: User

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:

 
 
  1. String MDYToDMY(String input)  
  2. {  
  3.    return Regex.Replace(intput, @"\b(?<month>\d{1,2})/(?<day>\d{1,2}/(?<year>\d{4})\b", "${day}-  
  4. ${month}-${year}");  
  5. }  

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} $

  1. Basic learning materials for. NET regular expressions
  2. What is a regular expression: its historical relationship with the. NET Framework
  3. C # Regular Expressions
  4. Use regular expressions to make C # determine whether the input date format is correct
  5. Use regular expressions in. NET in four cases

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.