asp.net API Regular Expression

Source: Internet
Author: User
Tags zip

In addition to the ASP tutorial. NET validation controls, most cases of using regular expressions in. NET use classes found in the system.text.regularexpressions namespace. Especially those you want to be familiar with, the main class regex,match , and matchcollection.

By the way, regular expression abbreviation style regex pronunciation is/reg-eks/or/rej-eks/, there are some controversies. I tend to the latter, but both kinds of pronunciation are approved by experts, so choose which pronunciation to decide by yourself.

The regex class has a large number of methods and properties that you may feel overwhelmed if you have not used it before. Some of the most common methods are summarized below:

Method

Description

Escape/unescape

The escape metacharacters in the string, used as text in an expression.

IsMatch

Returns "Ture" if a regular expression finds a match in an input string.

Match

If a match is found in the input string, the matching object is returned.

Matches

If any or all matches are found in the input string, the matching collection object is returned.

Replace

Replaces the match in the input string with the given replacement string.

Split

Splits an input string into a regular expression that matches a separate array element, returning an array string.

In addition to specifying many methods, there are several options that can be specified, usually in the Regex object constructor. Because these options are part of a bit mask, you might be able to specify these options at the same time (for example, you can specify both multiline and Singleline).

Method

Description

Compiled

This option is used when performing many matching operations in a loop. This saves the parsing expression steps for Each loop.

Multiline

It is not related to the number of rows in the input string. Rather, it only modifies the way of ^ and $ to match the start (BOL) and line endings (EOL) of the line, rather than the start and end of the entire input string.

IgnoreCase

Causes the pattern to ignore case when matching the search string.

Ignorepatternwhitespace

Allows any number of blank spaces to be included in the pattern as needed, and also supports the use of (? # comment #) syntax to add comments to the schema.

Singleline

It is not related to the number of rows in the input string. More precisely, it will lead to . (period) metacharacters match any character, not any character other than n (default).

Actions that are often performed with regular expressions include validation, matching, and substitution. In most cases, you can do this by using a static method of the Regex class, and you do not need to instantiate the regex class itself. To perform validation, all you have to do is to build or find the correct expression, and then use the IsMatch () method of the Regex class to apply the expression to the input string. For example, the following function demonstrates how to use a regular expression to validate a postal code:

private void Validatezipbutton_click (object sender, System.EventArgs e)
{
   string zipregex = @ "^d{5}$";
   if (Regex.IsMatch (Ziptextbox.text, Zipregex))
   {
      Resultlabel.text = "zip is valid!";
   }
   else
   {
      Resultlabel.text = ' zip is invalid! ';
   }
}

Similarly, you can use the static replace () method to replace a match with a specific string, as follows:

String NewText = Regex.Replace (inputstring, pattern, replacementtext);

Finally, you can use the following code to traverse the matching collection of the input string:

private void Matchbutton_click (object sender, System.EventArgs e)
{
   MatchCollection matches = regex.matches ( Searchstringtextbox.text, 
matchexpressiontextbox.text);
   Matchcountlabel.text = Matches.count.tostring ();
   Matcheslabel.text = "";
   foreach (match match in matches)
   {
      Matcheslabel.text + = "Found" + match.tostring () + "at 
position" + match.in Dex + ".<br>";
   }
}

Typically, you need to instantiate an instance of the Regex class when you need to specify a way outside of the default mode. Especially when setting options. For example, to create a regex instance that ignores case and pattern white space, and then retrieves a collection that matches the expression, you should use the following code:

Regex re = new regex (pattern, 
regexoptions.ignorecase | regexoptions.ignorepatternwhitespace);
MatchCollection mc = re.matches (inputstring);

The download file for this article includes the full use version of these samples, as in the simple asp.net tutorial page.

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.