[thelist] Regex Riddle

Jason Handby jason.handby at corestar.co.uk
Sat Aug 28 19:05:39 CDT 2010

> > Try this.
> >
> > Regular expression      ([^?&=]*)(?:&[^a]|=|&)
> >
> > Replacement expression  \1/
> Oops sorry, it was chopping out letters after ampersands. Try
> ([^?&=]*)(?:=|&|&)

...and now, having read your original email again, I realise that you
only want this to match if the URL is relative. I really should have
read more carefully to start with, shouldn't I?

I don't think that it's possible to have a regular expression that
matches all occurrences of = & and & in the querystring part of a
URL, but only if the URL begins with a certain string. It's certainly
possible to match and replace one occurrence, and if you're using a
programming language you could apply your expression to your URL string
repeatedly until it no longer matched, but I don't think you can do it
in one shot, just because of how regexes work...

When a regular expression engine is asked to find multiple matches in a
string, it moves along the string as it matches. The search for each
subsequent match starts where the previous one ended. So there's no way
to take the beginning of the string into account in the second and
subsequent matches, as it's no longer available.

But it's late at night, and maybe someone clever will come along and
correct me tomorrow :-)


More information about the thelist mailing list