in reply to Re: hex-only regex
in thread hex-only regex

Don't fall for the "dollar mistake" here. Remember that $ doesn't match end of string: it matches "end of string or just before a newline at end of string".

This means that you could have "123423453456456756786789789a89ab\n" in your string, and it'd still match. While this may not make any difference for your application, in some cases this could be a missed crucial check for a security validation, allowing a messy character where it doesn't belong (such as in a filename).

Beware the dollar. Use \z instead: /^[0-9a-f]{32}\z/i.

Replies are listed 'Best First'.
Re^3: hex-only regex
by jettero (Monsignor) on Jan 17, 2007 at 19:18 UTC
    Fascinating. I need to read perlre more often.

    Does m/regular$/s also work reliably or is \z the only safe way to do it? Until today, I've always used the former to change the meaning of $. UPDATE: Nope. It's not reliable at all. In fact, I'd go so far as to say it doesn't even work.

    -Paul

      /s makes dot match newline. The description "treat string as a single line" is, IMO, completely unhelpful.

      Caution: Contents may have been coded under pressure.