Created
August 11, 2012 23:22
-
-
Save marciol/3327781 to your computer and use it in GitHub Desktop.
PathToRegexForMicroWebFramework
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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