Regx explanation please

D

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.

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
 
R

Rainer Weikusat

Dave Saville said:
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.
 
W

Wolf Behrenhoff

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
 
D

Dave Saville

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.
 
P

Peter J. Holzer

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
 
D

Dave Saville

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
-" :)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top