nasty regex

I’m putting this here for documentation purposes… Because getting it right was a very frustrating ordeal (I’d never had to match both positively and negatively in the same regex before)

/^(?(?!.+\.php)^.*(\.jpg|\.jpeg|\.gif|\.ico|\.png)|^$)/s

what this is, essentially, saying is “true if the string doesnt match ^.+\.php and the string matches ^.*(\.jpg|\.jpeg|\.gif|\.ico|\.png)” The last bit: “|^$” never returns true in my case,because we’re matching on URI’s which are always at least one character long ( “/” )

Comments (5)

  1. Marc wrote::

    OW! That makes my head hurt!

    Wednesday, June 6, 2007 at 10:45 AM #
  2. apokalyptik wrote::

    yea… it made my head hurt too!!

    Wednesday, June 6, 2007 at 12:22 PM #
  3. Marc wrote::

    Thinking about is what makes my head hurt. On the other hand, just looking at it brings to mind the WordPress moto. "Code is poetry"

    Thursday, June 7, 2007 at 2:00 AM #
  4. I might be dense, but why do you need to check that it DOESN'T match .php if your asserting that it must match one of the other strings?

    /^(?.*.(jpe?g|gif|ico|png))?$/

    Wouldn't serve the same purpose and be a lot more readable?

    Sunday, July 29, 2007 at 9:50 AM #
  5. Dan wrote::

    I agree with Lachlan. A string can have one extension or the other, but not both. It'll be way easier to just match for what you're looking for. Incidentally, this could probably be simplified to:

    /.(?:jpe?g|gif|ico|png)z/

    If you Regexp engine doesn't support z, replace it with $. In 99% of the cases what you want is z though and not $ though. If you had a string like "logo.gifn", where the n is a newline, then using $ in the above regexp will actually match that string, where z will not.

    Saturday, October 20, 2007 at 5:06 PM #