ASP.NET Routing Regex to match specific pattern
I am trying to write a regular expression for ASP.NET MapPageRoute that matches a specific type of path.
I do not want to match anything with a file extension so I used this regex ^[^.]*$ which worked fine except it also picked up if the default document was requested. I do not want it to pick up the default document so I have been trying to change it to require at least one character. I tried adding .{1,} or .+ to the beginning of the working regex but it stopped working alltogether.
routes.MapPageRoute("content", "{*contentpath}", "~/Content.aspx", 开发者_如何转开发true, new RouteValueDictionary { }, new RouteValueDictionary { { "contentpath", @"^[^.]*$" } });
How can I change my regex to accomplish this?
Unfortunately my brain does not seem capable of learning regular expressions properly.
You want to change your *
quantifier to +
. *
matches zero or more times, whereas +
matches one or more. So, what you are asking for is this:
^[^.]+$
The regex is accomplishing this: "At the beginning of the string, match all characters that are not .
, at least one time, up to the end of the string."
^[^.]+$
zero is to * as one is to +
精彩评论