|
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 ();
}
}
} |