Front page | perl.perl5.porters |
Postings from February 2000
commify(), and regex bug (ugh)
Thread Next
From:
Jeff Pinyan
Date:
February 17, 2000 16:52
Subject:
commify(), and regex bug (ugh)
Message ID:
Pine.GSO.4.21.0002171937260.22615-100000@crusoe.crusoe.net
Ok, I got a new version of commify() for the perlfaq:
sub commify {
local $_ = shift;
s/\G(\d+?)(?=(?:\d\d\d)+(?:\.|$))/$1,/g;
return $_;
}
Just for documentation's sake, I'll explain the regex:
s{
\G # from where the last match ended
(\d+?) # some digits, but as few as possible
(?= # look ahead for:
(?:\d\d\d) # three digits; XXX SEE REGEX BUG BELOW
+ # one or more sets of triplets
(?:\.|$) # followed by a decimal point, or end-of-string
)
}{$1,}gx; # those digits, followed by a comma
Now, as for that regex bug.
Please examine the following code:
#!/usr/bin/perl -l
$, = ", ";
$_ = "abcdefghi";
print /(\w)(?=(\w{3})+$)/g;
print /(\w)(?=(\w\w\w)+$)/g;
print /(\w)(\w{3})+$/g;
print /(\w)(\w\w\w)+$/g;
And its output:
a, ghi, b, ghi, c, ghi, d, ghi, e, ghi, f, ghi, g, , h,
c, ghi, f, ghi
a, ghi
c, ghi
Why are (\w{3}) and (\w\w\w) behaving differently? And where is it
documented that
"abcdef" =~ /(\w)+/
sets $1 to "f"? I would have thought it would set it to "a". Can't get
around it with
"abcdef" =~ /^(\w)+/
either. Is that a bug, or a "feature"?
-- perl -V --
Summary of my perl5 (5.0 patchlevel 5 subversion 2) configuration:
Platform:
osname=solaris, osvers=2.5.1, archname=sun4-solaris
uname='sunos friday 5.5.1 generic_103640-22 sun4m sparc sunw,sparcstation-5 '
hint=recommended, useposix=true, d_sigaction=define
usethreads=undef useperlio=undef d_sfio=undef
Compiler:
cc='/usr/local/bin/gcc', optimize='-O', gccversion=2.7.2.3
cppflags='-I/usr/local/include'
ccflags ='-I/usr/local/include'
stdchar='unsigned char', d_stdstdio=define, usevfork=false
intsize=4, longsize=4, ptrsize=4, doublesize=8
d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=16
alignbytes=8, usemymalloc=y, prototype=define
Linker and Libraries:
ld='/usr/local/bin/gcc', ldflags =' -L/usr/local/lib -L/opt/gnu/lib'
libpth=/usr/local/lib /opt/gnu/lib /lib /usr/lib /usr/ccs/lib
libs=-lsocket -lnsl -ldb -ldl -lm -lc -lcrypt
libc=/lib/libc.so, so=so, useshrplib=false, libperl=libperl.a
Dynamic Linking:
dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags=' '
cccdlflags='-fPIC', lddlflags='-G -L/usr/local/lib -L/opt/gnu/lib'
Characteristics of this binary (from libperl):
Built under solaris
Compiled at Mar 24 1999 18:43:53
@INC:
/usr/local/lib/perl5/5.00502/sun4-solaris
/usr/local/lib/perl5/5.00502
/usr/local/lib/perl5/site_perl/5.005/sun4-solaris
/usr/local/lib/perl5/site_perl/5.005
.
--
MIDN 4/C PINYAN, NROTCURPI, US Naval Reserve japhy@pobox.com
http://www.pobox.com/~japhy/ http://pinyaj.stu.rpi.edu/
PerlMonth - An Online Perl Magazine http://www.perlmonth.com/
The Perl Archive - Articles, Forums, etc. http://www.perlarchive.com/
Thread Next
-
commify(), and regex bug (ugh)
by Jeff Pinyan