Skip to content

Instantly share code, notes, and snippets.

@marciol
Created August 11, 2012 23:22
Show Gist options
  • Save marciol/3327781 to your computer and use it in GitHub Desktop.
Save marciol/3327781 to your computer and use it in GitHub Desktop.
PathToRegexForMicroWebFramework
private Dictionary<string, string> PathForRegex(string path)
{
var dic = new Dictionary<string, Regex> {
{"foo", new Regex("foo/(?<directory_id>\\d+)/bar/(?<coast>\\d+)/hei", RegexOptions.ExplicitCapture)},
{"hei", new Regex("foo/(?<name>\\w+)/jow", RegexOptions.ExplicitCapture)}
};
var Params =
(
from tpl in dic
let rgx = tpl.Value
let match = rgx.Match(path)
where match.Success
select new KeyValuePair<Regex, Match>(rgx, match)
into rm
let rgx = rm.Key
let match = rm.Value
from name in rgx.GetGroupNames().Skip(1)
select new KeyValuePair<string, string>(name, match.Groups[name].Value)
);
return Params.ToDictionary(t => t.Key, u => u.Value);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment