Skip to content

danceink123/RegexMatcher

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

RegexMatcher

Regex Matching Library in C#

RegexMatcher is a library that maintains an internal dictionary of type <Regex, object>. Populate the dictionary with a series of Regex and the objects that should be returned when a match is found while evaluating some input.

For a sample app exercising the library please refer to the Test project.

Help or Feedback

Do you need help or have feedback? Contact me at joel at maraudersoftware.com dot com or file an issue here!

New in v1.0.1

  • Retarget to support both .NET Core 2.0 and .NET Framework 4.6.2.

Important Notes

Always add Regex and return object in order from most specific to least specific. The first match found will be used and the associated object will be returned.

Simple Example

using RegexMatcher;

static void Main(string[] args)
{
    Matcher matcher = new Matcher();

    // preload a few
    matcher.Add(new Regex("^/foo/\\d+$"), "foo with id");
    matcher.Add(new Regex("^/foo/?$"), "foo with optional slash");
    matcher.Add(new Regex("^/foo$"), "foo alone");
    matcher.Add(new Regex("^/bar/(.*?)/(.*?)/?$"), "bar with two children");
    matcher.Add(new Regex("^/bar/(.*?)/?$"), "bar with one child");
    matcher.Add(new Regex("^/bar/\\d+$"), "bar with id");
    matcher.Add(new Regex("^/bar/?$"), "bar with optional slash");
    matcher.Add(new Regex("^/bar$"), "bar alone");

    object val;
    if (matcher.Match("/bar/child/foo", out val))
    { // val is "bar with two children" }

    if (matcher.Match("/foo/36", out val))
    { // val is "foo with id" }

    if (matcher.Match("/unknown", out val)) { }
    else Console.WriteLine("Not found");
}

Regular Expression Notes

RegexMatcher uses standard C#/.NET regular expressions. I tested primarily against simple regular expressions with values that would be encountered as raw URLs/paths and it worked well.

Some notes that I found helpful which may help you too:

  • ^ is a starting anchor, useful when indicating that the pattern must be matched at the start of the input
  • $ is an ending anchor, useful when indicating that the pattern mst be matched at the end of the input
  • (.*?) will match any input string
  • \\d+ will match any number
  • \\ the escape character must be used when matching certain characters as a literal
  • ? marks the previous character or expression as optional

Helpful links:

Version History

Notes from previous versions (starting with v1.0.0) will be moved here.

v1.0.x

  • initial release

About

Regex Matching Library in C#

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 100.0%