Skip navigation

So in my effort to set free some of my useful code, today I am releasing a class called JHarris.RegexCollectioner.

I created this because I needed a class to check multiple regexs on a single string (its really increadibly trivial todo)

the code should be pritty self explanitory, but if you have any questions, feel free to ask

so heres the code:

using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace JHarris
{
public class RegexCollectioner
{
/// <summary>
/// Internal dictonary that holds the regexs
/// </summary>
Dictionary<string, string> m_RegexCollection = new Dictionary<string, string>();

/// <summary>
/// Adds a Regex to the collection
/// </summary>
/// <param name=”name”>the name of the regex</param>
/// <param name=”regex”>the regex</param>
public void addRegex(string name, string regex)
{
m_RegexCollection.Add(name, regex);
}

/// <summary>
/// Tests if a string matches a regex in the collection
/// </summary>
/// <param name=”input”>the string to test</param>
/// <returns>the name of the regex that works, if no regex is found to be a match, this function will return string.empty</returns>
public string FindRegex(string input)
{
string name = string.Empty;

foreach (KeyValuePair<string,string> rgx in m_RegexCollection)
{
if (Regex.IsMatch(input,rgx.Value))
{
name = rgx.Key;
break;
}
}

return name;
}

}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: