Face it, C# is not as regex-friendly as it could be. To do things like find a single regex match involves several lines or some nasty inline code. Using Extension methods, this can be simplified. In another addition to StatenUtil focused on regex, C# becomes a more coder-friendly language.
StringRegex.cs
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace StatenUtil.RegexExtensions
{
public static class StringRegex
{
public static string RegexReplace(this string source, string regex, string replacement)
{
Regex r = new Regex(regex);
return r.Replace(source, replacement);
}
public static string Match(this string source, string regex)
{
Regex r = new Regex(regex);
Match m = r.Match(source);
return m.Value;
}
public static string[] Matches(this string source, string regex)
{
Regex r = new Regex(regex);
MatchCollection mc = r.Matches(source);
List<string> matches = new List<string>();
foreach(object o in mc)
matches.Add(((Match)o).Value);
return matches.ToArray();
}
public static string RegexRemove(this string source, string regex)
{
Regex r = new Regex(regex);
return r.Replace(source, "");
}
}
}
This class offers four extension methods to the string class: Replace, Remove, Match first, and Match all. Aside from RegexReplace, the parameters often call for a single regular expression string.
Another addition that C# could use is a string scanner. A string scanner moves through a string one regex at a time matching the beginning of a string. This is useful for parsing languages in the way similar to JSON Parsing in Ruby.
StringScanner.cs
using System;
namespace StatenUtil.RegexExtensions
{
public class StringScanner
{
public string Remaining { get; set; }
public string Match { get; set; }
public StringScanner(string input)
{
Remaining = input;
}
public string Scan(string regex)
{
Match = Remaining.Match("^"+regex);
if (Match != null)
Remaining = Remaining.Substring(Match.Length);
return Match;
}
}
}
The StringScanner class constructor takes an input string that it will scan. Having only a Scan method, the class has the single use of moving through the input string as regex matches are made. As Scan finds a match, it returns the matching string. Also, the class has two properties: Remaining and Match. Remaining provides what is left in the string, useful for checking if the end of the input string has been met. Match provides the most recent match of the Scan method.