develooper Front page | perl.perl5.porters | Postings from February 2000

commify(), and regex bug (ugh)

Thread Next
Jeff Pinyan
February 17, 2000 16:52
commify(), and regex bug (ugh)
Message ID:
Ok, I got a new version of commify() for the perlfaq:

  sub commify {
    local $_ = shift;
    return $_;

Just for documentation's sake, I'll explain the regex:

    \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:
    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
    cc='/usr/local/bin/gcc', optimize='-O', gccversion=
    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/, 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

MIDN 4/C PINYAN, NROTCURPI, US Naval Reserve           
PerlMonth - An Online Perl Magazine  
The Perl Archive - Articles, Forums, etc.

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About