Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Regx explanation please

Reply
Thread Tools

Regx explanation please

 
 
Dave Saville
Guest
Posts: n/a
 
      07-26-2012
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
 
Reply With Quote
 
 
 
 
Rainer Weikusat
Guest
Posts: n/a
 
      07-26-2012
"Dave Saville" <(E-Mail Removed)> 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.

 
Reply With Quote
 
 
 
 
Wolf Behrenhoff
Guest
Posts: n/a
 
      07-26-2012
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

 
Reply With Quote
 
Dave Saville
Guest
Posts: n/a
 
      07-26-2012
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
 
Reply With Quote
 
Peter J. Holzer
Guest
Posts: n/a
 
      07-27-2012
On 2012-07-26 13:17, Dave Saville <(E-Mail Removed)> 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.velocityreviews.com/forums/(E-Mail Removed) |
__/ | http://www.hjp.at/ | -- Bill Code on (E-Mail Removed)
 
Reply With Quote
 
Dave Saville
Guest
Posts: n/a
 
      07-27-2012
On Fri, 27 Jul 2012 14:38:27 UTC, "Peter J. Holzer"
<(E-Mail Removed)> wrote:

> On 2012-07-26 13:17, Dave Saville <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
regx, global efficiencies? oldyork90 Javascript 4 06-10-2008 11:43 AM
Regx to remove all characters after a match Duke of Hazard Perl Misc 3 04-18-2008 11:43 AM
regx expression? (link included) werwer Perl Misc 2 02-02-2008 09:55 AM
how to format a return value by using re.sub(regx,rep1,str)? dongdong Python 2 03-25-2006 04:42 PM
regx for newlines -- var regX = /\\n/g; Jake Barnes Javascript 1 02-19-2006 09:49 AM



Advertisments