Giter Site home page Giter Site logo

regexmatcher's Introduction

RegexMatcher

NuGet Version NuGet

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.5

  • MatchPreference property to specify how to handle multiple match scenarios: first match, longest match, or shortest match

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");

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

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

    if (matcher.Match("/unknown", out object val3)) 
    { 
        // won't get here
    }
    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

Multiple Matches

The library behavior when multiple matches are found can be configured using the Matcher.MatchPreference property.

  • First returns the first match
  • LongestFirst will return the longest match
  • ShortestFirst will return the shortest match

Helpful links:

Version History

Refer to CHANGELOG.md for version history.

regexmatcher's People

Contributors

imrostami avatar jchristn avatar

Stargazers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.