tags:

views:

31

answers:

2

Hello, 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

+3  A: 

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

Robert P
+1 for advocating a better understanding.
Jason McCreary
By let through, I mean, match the RegEx
Shamil
+1, Although since we are not capturing anything, a non capturing group would be better, perhaps we can squeeze out some pre optimization from the engine also.
Anders
+1, more energy to explain then I had
Wrikken
@ct2k7: Ok, then *what* are you trying to match? How do you intend to rewrite the URLs? What should turn into what else? What's the input, and what's your desired output?
Robert P
hi Robert, the input is: www.site.com/opd/cache.html, and on the server, this is: www.site.com/index.php?do=opd/cache.html Rewriting is done using IIS' function mechanism plugin.
Shamil
+2  A: 

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!-)

Alex Martelli