Another RegEx question
I absolutely hate RegEx, I really need to learn it - it's so powerful. Here's the issue:
I'm trying to rewrite URLs in IIS, and I've got this default 开发者_高级运维RegEx:
^([^/]+)/?$
However, that does let things like this business/profile.html
through, but it lets business-profile.html
through.
How do I change it so that it lets the former through?
Thanks
If you want to understand and learn Regex, learn to break down their meaning when you're confused as to what they're doing. Here's the same regex, in expanded format.
^ # Start of the string
( # Take a group...
[^/]+ # of one or more characters (the +) that are NOT the / character
) # end of the group
/? # an optional '/'
$ # End of string
So this regex matches:
- All strings that don't have the
/
character in it - All strings that contain a single
/
at the end of it
To "fix" the regex, we need to know what you really mean by "let through". Do you mean "match the regex?"
(Side note: A great resource is http://www.regular-expressions.info/ - it provides a great cross-tool reference and tutorial for regex use.)
The original RE's purpose seems to be "forbid any URL with slashes inside" (one at the end is optionally allowed). If your purpose is "forbid absolutely nothing", ^(.*?)/?$
should work (with the *?
meaning non-greedy match -- RE dialects differ about such advanced things, so I don't know if yours will support it). @Wrikken has shown how to allow "at most one slash inside", and that clearly generalizes to "at most N slashes inside" for any fixed N. Without knowing exactly what you want to allow, and what to forbid, it's hard to be more helpful!-)
精彩评论