IsMaych
Public static void RunIsMatch ()
{
String inputString = "Welcome to the publishers, Wrox Pree Ltd ";
If (Regex. IsMatch (inputString, "wrox pree", RegexOptions. IgnoreCase) // check whether wrox pree exists in inputString. case insensitive.
{
Console. WriteLine ("Match Found ");
}
Else
{
Console. WriteLine ("No Match Found ");
}
}
# Region Replce () Usage
Public static void RunReplce ()
{
String input = "Welcome to the publishers, Wrox Pree Ltd ";
Input = Regex. Replace (input, "Wrox Pree", "wrox pree ");
Console. WriteLine (input );
String inputString = "123,456,123,123,789,123,888 ";
Regex regExp = new Regex ("123 ");
InputString = regExp. Replace (inputString, "xxx", 2, 4); // 4 indicates starting from 4th, and 2 indicates replacing up to two times
Console. WriteLine (inputString );
}
# Endregion
# Use of Split () in region Regular Expression
Public static void RunSplit ()
{
String inputString = "123, ABC, 456, DEF, 789 ";
String [] splitResults;
SplitResults = Regex. Split (inputString, ", |,"); // Split with commas (,) or commas)
StringBuilder resultBulider = new StringBuilder (32 );
Console. WriteLine (splitResults. Length );
Foreach (string stringElement in splitResults)
{
ResultBulider. AppendFormat ("{0 }:{ 1}", "af", stringElement );
}
Console. WriteLine (resultBulider. ToString ());
}
# Endregion
/// <Summary>
/// Match all numbers in the string
/// </Summary>
/// <Param name = "regexString"> Format of the string to be matched </param>
/// <Param name = "input"> string to be matched </param>
/// <Param name = "beginning"> enter the character position in the string to start searching. </Param>
/// <Param name = "length"> the substring contains the number of characters in the search. </Param>
Public void RunMatch (string regexString, string input, int beginning, int length)
{
Regex myRegex = new Regex (regexString );
Match matchMade = myRegex. Match (input, beginning, length );
If (matchMade. Length! = Length)
{
Console. Write ("{0}-{1} matching failed", beginning + 1, length );
}
Else
{
Console. WriteLine ("{0}-{1} matched successfully", beginning + 1, length );
}
# Region in the entire string, some of them match successfully. Use matchMade. Success
While (matchMade. Success)
{
Console. Write (matchMade. Value );
MatchMade = matchMade. NextMatch ();
}
# Endregion
}
MatchRegular MatchReg = new MatchRegular ();
String inPut = "123,456 ";
String regexString = @ "d +"; // match the number of 0-9 or more
MatchReg. RunMatch (regexString, inPut, 0, 4 );
# Region Groups and Capture usage
Public static void RunGroups ()
{
String string1 = "04:03:27 127.0.0.1 LibertyAssociates lily ";
Regex theReg = new Regex (@"(? <Time> (d | :) +) s "+ @"(? <Ip> (d |.) +) s "+ @"(? <Site> (S +) s) "+ @"(? <Site> (S + ))");
// If I use the same site group, a problem occurs. The group that follows will overwrite the previous group.
MatchCollection theMatches = theReg. Matches (string1 );
Foreach (Match theMatch in theMatches)
{
If (theMatch. Length! = 0)
{
Console. WriteLine ("theMatch: {0}", theMatch. ToString ());
Console. WriteLine ("time: {0}", theMatch. Groups ["time"]);
Console. WriteLine ("ip: {0}", theMatch. Groups ["ip"]);
Console. WriteLine ("site: {0}", theMatch. Groups ["site"]); // output lily
Console. WriteLine ("site: {0}", theMatch. Groups ["site"]); // output lily
Foreach (Capture cap in theMatch. Groups [