RegExp Help

I

Indigo5

This may be a simple question, but how would I use a regular expression to
convert reals to integers whenever possible. For example, if I had the
following lines

25.010 36.5 20.00
22.3 19. 35.


I would want those converted to

25.010 36.5 20
22.3 19 35

I know I can use something like:

s/\.$//g; to get rid of decimal points at the end of the string, but how do
I get rid of trailing zeros after the decimal point?

Would it be something like

s/\.(\d+)0$//g; ?
 
S

Scott Bryce

Indigo5 said:
This may be a simple question, but how would I use a regular expression to
convert reals to integers whenever possible. For example, if I had the
following lines

25.010 36.5 20.00
22.3 19. 35.


I would want those converted to

25.010 36.5 20
22.3 19 35

No regex needed:

use strict;
use warnings;

my $x1 = '1.9';
my $x2 = '2.00';
my $x3 = '3.';

print "x1 = $x1, x2 = $x2, x3 = $x3\n";

$x1 += 0;
$x2 += 0;
$x3 += 0;

print "x1 = $x1, x2 = $x2, x3 = $x3\n";
 
A

Andrew Hamm

Indigo5 said:
This may be a simple question, but how would I use a regular
expression to convert reals to integers whenever possible. For
example, if I had the following lines

25.010 36.5 20.00
22.3 19. 35.


I would want those converted to

25.010 36.5 20
22.3 19 35

From your sample output, you do not appear to want to convery 25.010 into
25.01 therefore I think you want this:

s/(\.0*)(?!\d)/' ' x length $1/eg;

I am guessing also that you want to maintain the column structure,
therefore I'm replacing the removed characters with the same amount of
spaces. If that is not important, then it's

s/(\.0*)(?!\d)//g;
 
A

Anno Siegel

BigDaDDY said:
Andrew,

My bad..I did want 25.010 to be converted to 25.01. Nice catch.

How does this change the regexp you provided?

"I mis-stated my question, so I can't use your reply. Send another."

Have you even made an attempt to correct it yourself?

[TOFU snipped]

Anno
 
S

Scott Bryce

BigDaDDY said:
Andrew,

My bad..I did want 25.010 to be converted to 25.01. Nice catch.

How does this change the regexp you provided?

Is there a reason why you insist on using a regex for a task that does
not require one?
 
A

Andrew Hamm

Scott said:
Is there a reason why you insist on using a regex for a task that does
not require one?

My guess is that he wants to retain the data in the original string;
perhaps massaging and cleaning up a report from some other external source
that he may not be able to fix.
 
A

Andrew Hamm

BigDaDDY said:
Andrew,

My bad..I did want 25.010 to be converted to 25.01. Nice catch.

How does this change the regexp you provided?

ok - welll, the way I think about it;

we want a string of zero's followed by a non-digit.

Adding in the recognition of the optional leading dot; well, it's an
optional leading dot! So the pattern starts to look like

(\.?0*)(?!\d)

the negative lookahead should ensure that this is recognised only at the
tail end of a number.

However, if there are any integers in the listing - ie without the dot,
then they might be destroyed; for example 100 would be converted to 1

If that is a risk; it will probably get much uglier. If it's not a
problem, I'll save time and not think about it unless you say it's needed.
Could get interesting solving that one and still keep it elegant.
 
S

Scott Bryce

Andrew said:
My guess is that he wants to retain the data in the original string;
perhaps massaging and cleaning up a report from some other external source
that he may not be able to fix.

That still wouldn't require a regex.

my $var_1 = '2.0';
my $var_2 = $var_1 + 0;

Use $var_2 in the report, and you still have the original data in $var_1.
 
A

Andrew Hamm

Scott said:
That still wouldn't require a regex.

my $var_1 = '2.0';
my $var_2 = $var_1 + 0;

Use $var_2 in the report, and you still have the original data in
$var_1.

Indigo5 submitted this data
25.010 36.5 20.00
22.3 19. 35.

so that looks like a pre-existing text file to me. That's why I think it's
too late to perform a fix where you suggest. Still, only Indigo knows for
sure, and it would be nice if Indigo could satisfy our curiosity with a
bit of info about where it's coming from - if you think it's important
enough to clear up. Otherwise assumptions rule the day.

If the task is a report massage then (s)he is not in control of the code
generating it (at a guess). If (s)he is writing the original text file
then of course fixing the source of the text files is more appropriate.
 
A

Andrew Hamm

Andrew said:
If the task is a report massage then (s)he is not in control of the

hmm - other reply was handled "BigDaddy" so I guess Indigo5 aka Big Daddy
is indeed a he.
 
S

Scott Bryce

Andrew said:
so that looks like a pre-existing text file to me.

No doubt.
That's why I think it's
too late to perform a fix where you suggest.

Huh?

I'm not suggesting fixing the file. I am only suggesting an easier way
to drop trailing zeroes to the right of the decimal than a regex.
Still, only Indigo knows for
sure, and it would be nice if Indigo could satisfy our curiosity with a
bit of info about where it's coming from

That doesn't matter. Once his script has the data, the source of the
data is irrelevant, if all he wants to do is strip trailing zeroes to
the right of the decimal.

Or did I miss something?

OK, taking another look at the original post, it looks like he wants to
retain the formatting of each line. If that is the case, you have a point.
 
A

Andrew Hamm

Scott said:
OK, taking another look at the original post, it looks like he wants
to retain the formatting of each line. If that is the case, you have
a point.

yup - that was my interpretation. It struck a chord for me because in the
business data world, that's the sort of crap we sometimes have to do;
process outside data which can be in as bad a format as a captured report.
I sincerely hope that XML will soon make report-scraping, CSV, "unload
files" and plain text files nothing but a fuzzy memory.

I'm also kind of wishing that there might be integers in the report which
need protecting against loss of trailing zeros. I'm letting my brain
"background" a solution because it's interesting. A bit of progress has
already popped up. I'm not sure it won't be another stepup of difficulty,
but am not willing to put the time in to play yet. I really shouldn't be
posting messages either...
 

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,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top