Charlie Calvert on Elvenware

Writing Code and Prose on Computers

Elvenware

Regular Expressions

Here is an example of using Regular Expressions in CSharp. For general information on Regular Expressions, go here.

public List<string> SimpleTest(string stringToParse)
{
    string expression = string.Format(@"^(Tom|tom)\s*is\s*(.*)$");
    Regex r = new Regex(expression);
    MatchCollection match = r.Matches(stringToParse);
    
    int mCount = 0;
    int gCount = 0;
    List<string> list = new List<string>();
    foreach (Match m in match)
    {
        list.Add(string.Format("Match: {0}", m));
        mCount++;
        foreach (Group g in m.Groups)
        {
            gCount++;                    
            list.Add(string.Format("Group Value: {0}", g.Value));
        }
    }
    return list;
}