I would not normally post such a simple question but:
- I'm no regex wiz.
- I'm under a time constraint (I need this before the end of today) and need to work on other aspects of the same project in the meantime.
- This is to go into production code so I'd prefer a more optimized, clean solution and not the quick, dirty ugly one below.
My current *debugging* code:
use strict;
while (<DATA>) {
chomp;
my $input = $_;
tr/$, +//d;
if (/^[-]?\d+$/) {
print "Acceptable: '$input' -> $_\n";
} elsif (/^[-]?\d+\.[\d]+$/) { ## problem
print "Acceptable: '$input' -> ", int $_, "\n";
} else {
print "Not acceptable: '$input' -> $_\n";
}
}
__DATA__
+30
-400
+20,0
-300.02
$ -50.1
-$ 500
- $ 60
+ $ 30.
All the example data is acceptable. I don't need a one-regex-fits-all solution--readability is more important. Rounding is unnecessary. Blanks outside of the numeric component are allowable but should be eliminated from the results. I'm not sure how to correct *problem* expression to handle the last test case (and it may allow undesirable things I'm not aware of). Here is the output from the above code:
Acceptable: '-400' -> -400
Acceptable: '+20,0' -> 200
Acceptable: '-300.02' -> -300
Acceptable: '$ -50.1' -> -50
Acceptable: '-$ 500' -> -500
Acceptable: '- $ 60' -> -60
Not acceptable: ' + $ 30.' -> 30.
Thanks in advance for any help!
--Jim
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.