Regx explanation please

Discussion in 'Perl Misc' started by Dave Saville, Jul 26, 2012.

  1. Dave Saville

    Dave Saville Guest

    Yesterday I needed to be able to print large numbers with commas. ie
    1,234,567

    Google turned up the following:

    1 while s/^(-?\d+)(\d{3})/$1,$2/;

    Two questions arise from this.

    1) I have not seen the "1 while........" construct before - How does
    that work? Or is it just syntatic suger to avoid empty curlies?

    2) How does that regex actually work? I get the followed by three
    digits bit - but its the -?\d+ bit I don't understand. And my camel
    book does not mention -? Nor does perlre. I am guessing that most of
    it is "look for at least one digit followed by exactly three digits.
    Change to what you found first, a comma, and then the three digits.
    The while keeps it going until the regex does nothing. I ran a few
    tests and it makes no difference if the -? is there or not. Or at
    least I could find no value where it did. The output is the same.

    TIA
    --
    Regards
    Dave Saville
     
    Dave Saville, Jul 26, 2012
    #1
    1. Advertising

  2. "Dave Saville" <> writes:
    > Yesterday I needed to be able to print large numbers with commas. ie
    > 1,234,567
    >
    > Google turned up the following:
    >
    > 1 while s/^(-?\d+)(\d{3})/$1,$2/;
    >
    > Two questions arise from this.
    >
    > 1) I have not seen the "1 while........" construct before - How does
    > that work? Or is it just syntatic suger to avoid empty curlies?


    The while ... is a statement modifier and since all of the actual code
    is in the condition, 'something' needs to be used as statement in
    front of it. In this case, it is 1 but it could really be anything
    which has no undesirable side-effects.

    >
    > 2) How does that regex actually work? I get the followed by three
    > digits bit - but its the -?\d+ bit I don't understand. And my camel
    > book does not mention -?


    This means 'the leading set of digits may be prefixed by a -', IOW, it
    also works for negative numbers.
     
    Rainer Weikusat, Jul 26, 2012
    #2
    1. Advertising

  3. Am 26.07.2012 15:17, schrieb Dave Saville:
    > Yesterday I needed to be able to print large numbers with commas. ie
    > 1,234,567
    >
    > Google turned up the following:
    >
    > 1 while s/^(-?\d+)(\d{3})/$1,$2/;
    >
    > Two questions arise from this.


    In perldoc, you can find (almost) exactly this regexp. And the first
    thing you see, it won't work for "+1234". :)

    perldoc -q comma
    ---> How can I output my numbers with commas added?


    - Wolf
     
    Wolf Behrenhoff, Jul 26, 2012
    #3
  4. Dave Saville

    Dave Saville Guest

    Thanks guys

    I see the perldoc one *does* work for plus and minus. Not that in my
    case it makes any difference as the numbers are computed totals which,
    when stringified, only get - signs if negative. ie you don't get a
    positive value stringifying to "+123". So it is only a problem if the
    number to convert is a signed string in the first place.
    --
    Regards
    Dave Saville
     
    Dave Saville, Jul 26, 2012
    #4
  5. On 2012-07-26 13:17, Dave Saville <> wrote:
    > 2) How does that regex actually work? I get the followed by three
    > digits bit - but its the -?\d+ bit I don't understand. And my camel
    > book does not mention -? Nor does perlre.


    The "-" isn't mentioned because it doesn't have any special meaning. It's
    just a literal "-". Perlre does explain "?". I'm fairly sure the camel
    book does, too.

    hp

    --
    _ | Peter J. Holzer | Deprecating human carelessness and
    |_|_) | Sysadmin WSR | ignorance has no successful track record.
    | | | |
    __/ | http://www.hjp.at/ | -- Bill Code on
     
    Peter J. Holzer, Jul 27, 2012
    #5
  6. Dave Saville

    Dave Saville Guest

    On Fri, 27 Jul 2012 14:38:27 UTC, "Peter J. Holzer"
    <> wrote:

    > On 2012-07-26 13:17, Dave Saville <> wrote:
    > > 2) How does that regex actually work? I get the followed by three
    > > digits bit - but its the -?\d+ bit I don't understand. And my camel
    > > book does not mention -? Nor does perlre.

    >
    > The "-" isn't mentioned because it doesn't have any special meaning. It's
    > just a literal "-". Perlre does explain "?". I'm fairly sure the camel
    > book does, too.


    But of course I was looking for both characters together :-( I think I
    have got so used to ?'s near the front of a regex doing something
    strange that it never occurred to me that is was actually "none or 1
    -" :)

    --
    Regards
    Dave Saville
     
    Dave Saville, Jul 27, 2012
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dongdong
    Replies:
    2
    Views:
    298
    Paul McGuire
    Mar 25, 2006
  2. werwer

    regx expression? (link included)

    werwer, Feb 2, 2008, in forum: Perl Misc
    Replies:
    2
    Views:
    111
    Brian McCauley
    Feb 2, 2008
  3. Duke of Hazard

    Regx to remove all characters after a match

    Duke of Hazard, Apr 18, 2008, in forum: Perl Misc
    Replies:
    3
    Views:
    185
    Gunnar Hjalmarsson
    Apr 18, 2008
  4. Jake Barnes

    regx for newlines -- var regX = /\\n/g;

    Jake Barnes, Feb 19, 2006, in forum: Javascript
    Replies:
    1
    Views:
    91
    Janwillem Borleffs
    Feb 19, 2006
  5. oldyork90

    regx, global efficiencies?

    oldyork90, Jun 9, 2008, in forum: Javascript
    Replies:
    4
    Views:
    102
    oldyork90
    Jun 10, 2008
Loading...

Share This Page