nasty regex

Gravatar

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


Posted on : Jun 05 2007
Posted under Business, Personal, Random Thoughts, Software Development, web stuff |

5 People have left comments on this post

Jun 6, 2007 - 06:06:18
Gravatar  Marc said:

OW! That makes my head hurt!

Jun 6, 2007 - 08:06:29
Gravatar  apokalyptik said:

yea… it made my head hurt too!!

Jun 7, 2007 - 10:06:14
Gravatar  Marc said:

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”

Jul 29, 2007 - 05:07:19
Gravatar  Lachlan Donald said:

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?

Oct 21, 2007 - 01:10:46
Gravatar  Dan said:

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.gif\n”, where the \n is a newline, then using $ in the above regexp will actually match that string, where \z will not.