Page 1 of 1

Pretty URL matching (omitting particular urls)

Posted: Tue May 12, 2009 9:59 am
by Glycerine
I have a mod rewite of which works:

Code: Select all

^.*?(apple.+)$
will match

http://www.strangemother.com/pages/apple/food

great - but then it also matches

http://www.strangemother.com/images/apple

You know a way to not match if the url has 'images' within it - I kinda know its a negative lookbehind - but from then on, I struggle...

Re: Pretty URL matching (omitting particular urls)

Posted: Wed May 13, 2009 3:41 am
by prometheuzz
No, not negative look behind. Most PCRE engines will not allow "variable length look behinds", which means that you can't say: "look behind zero or more character and see if the word 'images' is there".
But, of course, you can use look ahead for this just as well:

Code: Select all

^(?=.*apple)(?!.*images).*$
which will match any string (without new line characters) containing the word "apple" (positive look ahead) and NOT containing the word "images" (negative look ahead).

Re: Pretty URL matching (omitting particular urls)

Posted: Wed May 13, 2009 3:57 am
by Glycerine
I dunno how you ever got your head around this stuff...

Re: Pretty URL matching (omitting particular urls)

Posted: Wed May 13, 2009 7:47 am
by prometheuzz
Glycerine wrote:I dunno how you ever got your head around this stuff...
If you break it down, it ain't that hard*:

Code: Select all

^           // start of the string
(?=         // start positive look ahead
  .*        //   zero or more characters other than a new line
  apple     //   match the string 'apple'
)           // end positive look ahead
(?!         // start negative look ahead
  .*        //   zero or more characters other than a new line
  images    //   match the string 'images'
)           // end negative look ahead
.*          // zero or more characters other than a new line
$           // end of the string
* Perhaps I am not the best judge of when a regex is easy or not... ;)