Regex Gripping Documentation

I really need to get my head around Regex. I’ve often needed an expression and ended up on here asking someone else to provide it (thanks Kem).

Every time I google regex and find any documentation on the subject I normally drop to sleep in the first paragraph. Does anyone know of any well written hands on documentation to get me started?

Mike, if you can get through the first few paragraphs you’ll be golden. They really are not that hard. Get the docs and pickup RegExRX to play around with the expressions easily. You’ll be a whiz before you know it.

I write a column on the subject in xDev, if that helps. Also check out

http://regular-expressions.info

No it’s not going in. I’m full of cold too so i’m being lazy. Perhaps someone can help and i’ll put learning regex on the back burner for next week. I am trying to put together a mod rewrite rule. My URL will be of the form:

www.example.com/controller/command/parameters

the re-written url needs to be of the form

index.php?c=controller&cmd=command&params=parameters

controller and command will be a single alphanumeric word, parameters could be id/4/cat/6 etc.

Other forms the url may take are:

www.example.com/
www.example.com/controller
www.example.com/controller/command

all will redirect to index.php.

What I have so far is below however this does not deal with the parameters part.

RewriteRule ^([^/]*)(.*)$ index.php?c=$1&cmd=$2 [L,QSA]

Any help would be greatly appreciated,

[quote=88081:@Mike Charlesworth]What I have so far is below however this does not deal with the parameters part.

RewriteRule ^([^/]*)(.*)$ index.php?c=$1&cmd=$2 [L,QSA]

Any help would be greatly appreciated,[/quote]

Did you grab a copy of RegExRX? You’re so close. You simply forgot the / in between your two groups.

Also, when working with mod_rewrite, create multiple rules. For example:

RewriteRule ^([^/]*)/(.*)$ index.php?c=$1&cmd=$2 [L,QSA]
RewriteRule ^([^/]*)$ index.php?c=$1&cmd=default [L,QSA]

BTW… RegEx’s are not that difficult, but mod_rewrite is voodoo :slight_smile:

OH, your parameters should be an additional rule, in case you were wondering.

Jeremy, thanks, I think I’ve cracked it now. As you said i missed my / between groups. Also as you advised I have used multiple rules to rewrite the various URL formats . Should I put a flag in to stop processing. I am using [L] flag to stop processing further rules if the rule matches which seems to work. Is this correct?

Anyway my rules end up as:

RewriteRule ^([^/]*)$ index.php?controller=$1 [NC,L] RewriteRule ^([^/]*)/([^/]*)$ index.php?controller=$1&cmd=$2 [NC,L] RewriteRule ^([^/]*)/([^/]*)/(.*)$ index.php?controller=$1&cmd=$2&params=$3 [NC,L]

Yes it is voodoo, perhaps I should have mentioned I was writing mod rewrite rules in my OP and not just trying to get my head around regex.