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 ( “/” )

5 thoughts on “nasty regex

  1. 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"

  2. 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?

  3. Dan says:

    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.

Leave a Reply