Home > Archive > PERL Miscellaneous > November 2005 > Making sure a string is valid money number
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Making sure a string is valid money number
|
|
| stevob@gmail.com 2005-11-27, 6:58 pm |
| I am trying to make sure a string that I have is in the format xxxxx.xx
(where x are digits only, and there can be any number of digits before
the . but only two after.) I have tried many different regex
combinations and I can't seem to get it to work. Here is what I have
most rescently that I think should work, but doesn't:
$amount=~/^[0-9]+(\.[0-9]{2})$/
Here is my thinking:
^[0-9]+ means that the begining of the string can only contain digits,
but as many as desired.
(\.[0-9]{2})$ means that, must contain a decimal and then only two
digits after that decimal at the end of the string.
Any help is appreciated.
| |
| Purl Gurl 2005-11-27, 6:58 pm |
| stevob wrote:
(snipped)
> I am trying to make sure a string that I have is in the format xxxxx.xx
> (where x are digits only, and there can be any number of digits before
> the . but only two after.) I have tried many different regex
> combinations and I can't seem to get it to work.
#!perl
$string = 123456.78;
if ((length(substr($string, rindex ($string, "."))) == 3) && (($string =~ tr/0-9//) == (length($string) - 1)))
{ print "Ok Boss"; }
Purl Gurl
| |
| Paul Lalli 2005-11-27, 6:59 pm |
| stevob@gmail.com wrote:
> I am trying to make sure a string that I have is in the format xxxxx.xx
> (where x are digits only, and there can be any number of digits before
> the . but only two after.) I have tried many different regex
> combinations and I can't seem to get it to work. Here is what I have
> most rescently that I think should work, but doesn't:
> $amount=~/^[0-9]+(\.[0-9]{2})$/
I see nothing particularly wrong [1] with that regexp, as it seems to
fit your description. Perhaps you could post a short-but-complete
script which demonstrates how it fails? (Have you read the Posting
Guidelines for this group?)
[1] The [0-9] tokens could be simplified to \d tokens, and the parens
are wholly unneeded, but neither of these are "wrong" per se.
Paul Lalli
| |
| Eric J. Roode 2005-11-27, 9:56 pm |
| stevob@gmail.com wrote in news:1133124499.039707.91950
@g44g2000cwa.googlegroups.com:
> I am trying to make sure a string that I have is in the format xxxxx.xx
> (where x are digits only, and there can be any number of digits before
> the . but only two after.) I have tried many different regex
> combinations and I can't seem to get it to work. Here is what I have
> most rescently that I think should work, but doesn't:
> $amount=~/^[0-9]+(\.[0-9]{2})$/
> Here is my thinking:
> ^[0-9]+ means that the begining of the string can only contain digits,
> but as many as desired.
> (\.[0-9]{2})$ means that, must contain a decimal and then only two
> digits after that decimal at the end of the string.
First, I would suggest using an existing wheel instead of reinventing:
use Regexp::Common;
$amount =~ /^$RE{num}{real}{-places=>2}$/;
However, if you wish to roll your own, presumably as a learning exercise,
that's fine too.
$amount=~/^[0-9]+(\.[0-9]{2})$/
This looks fine to me, other than that the parentheses are unnecessary.
You say it didn't work. That's not much information to go on. What
"didn't work"?
--
Eric
`$=`;$_=\%!;($_)=/(.)/;$==++$|;($.,$/,$,,$\,$",$;,$^,$#,$~,$*,$:,@%)=(
$!=~/(.)(.).(.)(.)(.)(.)..(.)(.)(.)..(.)......(.)/,$"),$=++;$.++;$.++;
$_++;$_++;($_,$\,$,)=($~.$"."$;$/$%[$?]$_$\$,$:$%[$?]",$"&$~,$#,);$,++
;$,++;$^|=$";`$_$\$,$/$:$;$~$*$%[$?]$.$~$*${#}$%[$?]$;$\$"$^$~$*.>&$=`
| |
| Matt Garrish 2005-11-27, 9:56 pm |
|
"Purl Gurl" <purlgurl@purlgurl.net> wrote in message
news:rLydnSkG8PRyuBfeRVn-iA@giganews.com...
> stevob wrote:
>
> (snipped)
>
>
> #!perl
>
> $string = 123456.78;
>
> if ((length(substr($string, rindex ($string, "."))) == 3) && (($string =~
> tr/0-9//) == (length($string) - 1)))
>
You should write a book on how to write needlessly verbose code to avoid
having to understand regular expressions. It'd probably be a hit in the VB6
world.
Matt
| |
| Matt Garrish 2005-11-27, 9:56 pm |
|
"Eric J. Roode" <sdn.girths00869@zoemail.net> wrote in message
news:Xns971BD2B3DD69Esdn.comcast@216.196.97.136...
> stevob@gmail.com wrote in news:1133124499.039707.91950
> @g44g2000cwa.googlegroups.com:
>
>
> First, I would suggest using an existing wheel instead of reinventing:
>
> use Regexp::Common;
>
> $amount =~ /^$RE{num}{real}{-places=>2}$/;
>
> However, if you wish to roll your own, presumably as a learning exercise,
> that's fine too.
>
> $amount=~/^[0-9]+(\.[0-9]{2})$/
>
But is there any need in this case to install a module and learn a new
syntax to replace a simple regular expression? Expecially since there was
nothing wrong with the code the OP posted?
Matt
| |
| Eric J. Roode 2005-11-29, 3:57 am |
| "Matt Garrish" <matthew.garrish@sympatico.ca> wrote in
news:aZtif.1416$wf2.122504@news20.bellglobal.com:
>
> But is there any need in this case to install a module and learn a new
> syntax to replace a simple regular expression? Expecially since there
> was nothing wrong with the code the OP posted?
No, not particularly. I just dislike reinventing wheels, and writing code
from scratch when someone else has done the same thing already.
--
Eric
`$=`;$_=\%!;($_)=/(.)/;$==++$|;($.,$/,$,,$\,$",$;,$^,$#,$~,$*,$:,@%)=(
$!=~/(.)(.).(.)(.)(.)(.)..(.)(.)(.)..(.)......(.)/,$"),$=++;$.++;$.++;
$_++;$_++;($_,$\,$,)=($~.$"."$;$/$%[$?]$_$\$,$:$%[$?]",$"&$~,$#,);$,++
;$,++;$^|=$";`$_$\$,$/$:$;$~$*$%[$?]$.$~$*${#}$%[$?]$;$\$"$^$~$*.>&$=`
| |
| foo bar baz qux 2005-11-29, 6:59 pm |
| Matt Garrish wrote:
> "Purl Gurl" <purlgurl@purlgurl.net> wrote in message
> news:rLydnSkG8PRyuBfeRVn-iA@giganews.com...
>
> You should write a book on how to write needlessly verbose code to avoid
> having to understand regular expressions. It'd probably be a hit in the VB6
> world.
>
But wait, Kira[1] loves useless benchmarks, lets try one:
Benchmark: timing 10000000 iterations of callgirl, steveob...
callgirl: 17 wallclock secs (16.22 usr + 0.00 sys = 16.22 CPU) @
616598.84/s
steveob: 15 wallclock secs (14.41 usr + 0.00 sys = 14.41 CPU) @
694155.21/s
Gee Kira's code is slower[2] as well as being needlessly verbose.
[1] Kiralynne Schilitubi AKA callgirl AKA Godzilla AKA Purl Gurl etc
[2] Kira really cares about shaving nanoseconds off her run times and
frequently uses this to hotly justify Kira's peculiar coding practices.
|
|
|
|
|