namespace RegularExpression
{
///
/// this method only match the first position
///
class Program
{
static void Main(string[] args)
{
string input= "the quick brown fox jumped over the lazy dog";
Regex reg = new Regex("the");//define the match metacharacter
Match matchSet;
int matchPos;
matchSet = reg.Match(input);//veryfy the string with the metacharcters
if (matchSet.Success)//if there's matched string, return true
{
matchPos = matchSet.Index;
Console.WriteLine("found the match at position: "+matchPos);
}
}
}
}
***************************************************
using System.Text.RegularExpressions;//
namespace RegularExpression
{
///
/// this method match mutiple position
///
class Program
{
static void Main(string[] args)
{
string input= "the quick brown fox jumped over the lazy dog";
Regex reg = new Regex("the");//define the match metacharacter
MatchCollection matchSet;
matchSet = reg.Matches(input);
if (matchSet.Count > 0)
{
foreach (Match aMatch in matchSet)
{
Console.WriteLine("found a match at: "+aMatch.Index);
}
}
Console.WriteLine();
}
}
}
**********************************************
using System.Text.RegularExpressions;//
namespace RegularExpression
{
///
/// regular expression also has a Replace method
///
class Program
{
static void Main(string[] args)
{
string input= "the quick brown fox jumped over the lazy dog";
Regex reg = new Regex("the");//define the match metacharacter
input = Regex.Replace(input, "brown", "black");
Console.WriteLine(input);
}
}
}
*****************************
using System.Text.RegularExpressions;//
namespace RegularExpression
{
///
///
///
class Program
{
static void Main(string[] args)
{
string[] words = new string[]{"bad","boy","baaad","bear","bend","ba"};
foreach (string word in words)
{
if (Regex.IsMatch(word, "ba+"))
{
Console.WriteLine(word);
}
}
}
}
}
*****************************
using System.Text.RegularExpressions;//
namespace RegularExpression
{
///
///
///
class Program
{
static void Main(string[] args)
{
string[] words = new string[]{"bad","boy","baaad","bear","bend","baad"};
foreach (string word in words)
{
if (Regex.IsMatch(word, "ba{2}d"))
{
Console.WriteLine(word);
}
}
}
}
}
********************************
using System.Text.RegularExpressions;//
namespace RegularExpression
{
///
///
///
class Program
{
static void Main(string[] args)
{
string[] words = new string[]{"part","of","this","string","is","bold"};
//string regExp = "<.*>";//this is a kind of expression
//string regExp = "<.+>";//
string regExp = "<.+?>";//try these three expressions
MatchCollection aMatch;
foreach (string word in words)
{
if (Regex.IsMatch(word, regExp))
{
aMatch = Regex.Matches(word,regExp);
for (int i = 0; i < aMatch.Count; i++)
{
Console.WriteLine(aMatch[i].Value);
}
}
}
}
}
}
***********************************
using System.Text.RegularExpressions;//
namespace RegularExpression
{
///
///
///
class Program
{
static void Main(string[] args)
{
string input = "the quick brown fox jumped over the lazy dog";
MatchCollection matchSet;
//matchSet = Regex.Matches(input,".");//out put all characters index
matchSet = Regex.Matches(input, "t.e");//out put two "the" characters index
foreach (Match aMatch in matchSet)
{
Console.WriteLine("matches at: "+aMatch.Index);
}
}
}
}
**************************************
using System.Text.RegularExpressions;//
namespace RegularExpression
{
///
///
///
class Program
{
static void Main(string[] args)
{
string input = "THE quick BROWN FOX JUMPED OVER THE LAZY dog";
MatchCollection matchSet;
//matchSet = Regex.Matches(input,".");//out put all characters index
matchSet = Regex.Matches(input, "[a-z]");//out put two "the" characters index
foreach (Match aMatch in matchSet)
{
Console.WriteLine("matches at: "+aMatch.Index);
}
}
}
}
***************************************
using System.Text.RegularExpressions;//
namespace RegularExpression
{
///
///
///
class Program
{
static void Main(string[] args)
{
string[] words = new string[] { "heal","heel","noah","techno"};
//string regExp = "^h";//match at the beginning of word
string regExp = "h$";//match at the end of word
Match aMatch;
foreach (string word in words)
{
if (Regex.IsMatch(word, regExp))
{
aMatch = Regex.Match(word, regExp);
Console.WriteLine("Matched: "+word+" at position: "+aMatch.Index);
}
}
}
}
}
**************************************
using System.Text.RegularExpressions;//
namespace RegularExpression
{
///
///
///
class Program
{
static void Main(string[] args)
{
string words = "08/14/1957 46 02/12/1959 45 08/09/1985 18 03/25/1988 15
09/09/1990 13";
string regExp = "(\\s\\d{2}\\s)";//method to get the age
string regBirthDay = "(?
birth day
MatchCollection matchSet = Regex.Matches(words,regExp);
foreach (Match aMatch in matchSet)
{
Console.WriteLine(aMatch.Groups[0].Captures[0]);
}
MatchCollection matchSetDate = Regex.Matches(words,regBirthDay);
foreach (Match amatch in matchSetDate)
{
Console.WriteLine("Date: "+amatch.Groups["dates"]);
}
}
}
}


No comments:
Post a Comment