views:

29

answers:

1

heya,

I'm attempting to write a mod_rewrite rule to remap some friendly URLs for searches, and my regex-fu isn't that good...lol.

Basically, in the URL, after the subdomain, there will be a variable number of words (\w), separated by a character (say +). An empty expression is allowed (i.e. "foo.bar.com"), and then after that, something like "foo.bar.com/birds", or "foo.bar.com/birds+cats+fishes").

From this, we need to take the words, and pass them as query strings. How exactly would you go about splitting up an arbitary number of matches, and then passing them on to our PHP page? Now, if the separator was just "+", well, we could just pass this on as a whole discrete chunk to the page, as that uses + as it's separator character anyway. However, I'm curious - if it wasn't, how would you split it up? Is it possible with just a mod_rewrite rule? Or would you have to use something else?

Cheers, Victor

A: 

As long as your separators are all non-alpha characters you could just do greedy matching for ([A-Za-z]+)* (basically the same thing as (\w+)*), and that should net you groups of all the words broken up by other characters in between.

Gabriel Hurley