Regex to match odd numbers

Abigail abigail at abigail.be
Sun Aug 24 00:23:42 BST 2014


On Sat, Aug 23, 2014 at 04:43:06PM +0100, Aaron Crane wrote:
> Paul Makepeace <paulm at paulm.com> wrote:
> > Does anyone have any concrete examples where the locale affecting
> > meaning/matching of \d causes real problems?
> 
> In my experience, it's not necessarily the locale so much as Unicode
> characters that the programmer wasn't expecting, which then cause
> surprising behaviour. For example, this looks more-or-less sensible:
> 
> say $arg + 17 if $arg =~ /\A\d+\z/
> 
> but if $arg is a digit other than 0..9, Perl will treat it as 0 and
> emit a warning. (Which is particularly problematic if you also have
> fatal warnings enabled.)


Using [0-9] is much safer, but there are still gotchas. I've been
bitten by:

    my ($value) = $str =~ /([0-9]+)/;
    if ($value) {  # Don't divide by 0
        $something /= $value;
    }

My program crashed because it was dividing by zero... /[0-9]+/ matches
"00", which is true, but numerically still 0.



Abigail


More information about the london.pm mailing list