Application Example of common Regular Expressions in asp.net

Source: Internet
Author: User

This article introduces the application of common Regular Expressions in asp.net. For more information, see.

The Code is as follows: Copy code

Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. Text. RegularExpressions;
Using System. Drawing;

Namespace RegularExpressionConsoleApplication
{
Class Program
{


Static void Main (string [] args)
{
// LoopBackMatch (); // backtracing (involving a subexpression)
// LoopForwardMatch (); // forward match (? =)
// LoopAfterwardMatch (); // backward match (? <=)
// LoopForwardAndAfterwardMatch (); // combine forward and backward matching
ConditionItems (); // backtracking matching of embedding Conditions
}

/// <Summary>
/// Forward match
/// </Summary>
Static void LoopForwardMatch ()
{
String text = "http://www.cnblogs.com ";
String expression = @ ". + (? = :)";
Regex reg = new Regex (expression, RegexOptions. IgnoreCase );
MatchCollection mc = reg. Matches (text );
Foreach (Match match in mc)
{
Console. WriteLine (match. Value );
}

Console. ReadKey ();
}

/// <Summary>
/// Backward match
/// </Summary>
Static void LoopAfterwardMatch ()
{
String text = "ABC01: $12.56 ";
String expression = @"(? <= $) [0-9.] + ";
Regex reg = new Regex (expression, RegexOptions. IgnoreCase );
MatchCollection mc = reg. Matches (text );
Foreach (Match match in mc)
{
Console. WriteLine (match. Value );
}

Console. ReadKey ();
}

/// <Summary>
/// Combine backward and forward matching
/// </Summary>
Static void LoopForwardAndAfterwardMatch ()
{
StringBuilder sbStr = new StringBuilder ();
SbStr. Append ("<Head> ");
SbStr. Append ("rn ");
SbStr. Append ("<Title> Ben Forta's HomePage </Title> ");
SbStr. Append ("rn ");
SbStr. Append ("</Head> ");

String expression = @"(? <= <Title> ).*? (? = </Title> )";
Regex reg = new Regex (expression, RegexOptions. IgnoreCase );
MatchCollection mc = reg. Matches (sbStr. ToString ());
Foreach (Match match in mc)
{
Console. WriteLine (match. Value );
}
Console. ReadKey ();
}

/// <Summary>
/// Note that the Backtracking matching method is used here.
/// </Summary>
/// <Param name = "args"> </param>
Static void LoopBackMatch ()
{
String text = "this is a text, and I want to know if there is any repeated words in it .";
String expression = @ "[] + (w +) [] + 1 ";
Regex reg = new Regex (expression, RegexOptions. IgnoreCase );
MatchCollection mc = reg. Matches (text );
Foreach (Match match in mc)
{
Console. WriteLine (match. Value );
}

Console. ReadKey ();
}

/// <Summary>
/// Embedding condition. The text to be matched is as follows:
/*************************************** ******************
* <! -- Nav Bar -->
* <TD>
* <A href = '/home'> </a>
*
* <A href = '/search'> </a>
*
* <A href = '/help'> </a>
* </Td>
**************************************** *****************/
/// </Summary>
Static void ConditionItems ()
{
StringBuilder text = new StringBuilder ();
Text. Append ("<! -- Nav Bar --> ");
Text. Append ("rn ");
Text. Append ("<TD> ");
Text. Append ("rn ");
Text. Append ("<a href = '/home'> </a> ");
Text. Append ("rn ");
Text. Append (" ");
Text. Append ("rn ");
Text. Append ("<a href = '/search'> </a> ");
Text. Append ("rn ");
Text. Append (" ");
Text. Append ("rn ");
Text. Append ("<a href = '/help'> </a> ");
Text. Append ("rn ");
Text. Append ("</td> ");

String expression = @ "(<as + [^>] +> s *)? ] +> (? (1) s * </a> )";
Regex reg = new Regex (expression, RegexOptions. IgnoreCase );
MatchCollection mc = reg. Matches (text. ToString ());
// Foreach (Match match in mc)
//{
// Console. WriteLine (match. Value );
//}

Console. ForegroundColor = ConsoleColor. Yellow;
Console. WriteLine ("the total count of match result is:" + mc. Count );

For (int I = 0; I <mc. Count; I ++)
{
Match match = mc [I];

Console. ForegroundColor = ConsoleColor. Red;

Console. WriteLine ("|-Match [" + I + "] is:" + match. Value );

GroupCollection groups = match. Groups;
For (int j = 0; j <groups. Count; j ++)
{
Console. ForegroundColor = ConsoleColor. DarkGreen;

Console. WriteLine ("| --- Groups [" + j + "] is:" + groups [j]);
}
}
Console. ReadKey ();
}
}
}

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.