Regex to match odd numbers

Paul Makepeace paulm at paulm.com
Sat Aug 23 00:47:06 BST 2014


$thread->resurrect();


On Tue, May 27, 2014 at 12:37 PM, Mark Fowler <mark at twoshortplanks.com> wrote:
>
> On Tuesday, May 27, 2014, Sam Kington <sam at illuminated.co.uk> wrote:
> >
> > Sounds like you want something like
> >
> > / ( ^ 5[.] ( [79] | \d+ [13579] ) ) /x
> >
>
> This is where I mention that \d matches characters other than [0-9] unless
> you have the /a flag in effect (thanks Unicode!)

Does anyone have any concrete examples where the locale affecting
meaning/matching of \d causes real problems?

I'm assuming the worst case is it matches too much, e.g. picks up
spurious Chinese numerals, which seems like a wildly improbable edge
case for most datasets+patterns. Presumably there isn't a situation
where \d _doesn't_ match [0-9] at least? In other words [0-9] is a
subset of \d for all locales.

$ export LC_CTYPE=zh_CN.utf-8
$ perl -Mlocale -Mutf8 -le 'print "一" =~ /\d/'  # 1

Doesn't print 1 - why?

$ export LC_CTYPE=zh_CN.utf-8
$ perl -Mlocale -Mutf8 -le 'print "三" =~ /[一-六]/'  # 3 in 1-6? Yes
1
$ export LC_CTYPE=en_US.utf-8
$ perl -Mlocale -Mutf8 -le 'print "三" =~ /[一-六]/'
1

Why is it still 1? OS X with Perl 5.16.2

Paul



More information about the london.pm mailing list