Making sure a string is valid money number

S

stevob

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

Paul Lalli

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
 
E

Eric J. Roode

(e-mail address removed) wrote in @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
`$=`;$_=\%!;($_)=/(.)/;$==++$|;($.,$/,$,,$\,$",$;,$^,$#,$~,$*,$:,@%)=(
$!=~/(.)(.).(.)(.)(.)(.)..(.)(.)(.)..(.)......(.)/,$"),$=++;$.++;$.++;
$_++;$_++;($_,$\,$,)=($~.$"."$;$/$%[$?]$_$\$,$:$%[$?]",$"&$~,$#,);$,++
;$,++;$^|=$";`$_$\$,$/$:$;$~$*$%[$?]$.$~$*${#}$%[$?]$;$\$"$^$~$*.>&$=`
 
M

Matt Garrish

Purl Gurl said:
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
 
M

Matt Garrish

Eric J. Roode said:
(e-mail address removed) wrote in @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})$/

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
 
E

Eric J. Roode

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
`$=`;$_=\%!;($_)=/(.)/;$==++$|;($.,$/,$,,$\,$",$;,$^,$#,$~,$*,$:,@%)=(
$!=~/(.)(.).(.)(.)(.)(.)..(.)(.)(.)..(.)......(.)/,$"),$=++;$.++;$.++;
$_++;$_++;($_,$\,$,)=($~.$"."$;$/$%[$?]$_$\$,$:$%[$?]",$"&$~,$#,);$,++
;$,++;$^|=$";`$_$\$,$/$:$;$~$*$%[$?]$.$~$*${#}$%[$?]$;$\$"$^$~$*.>&$=`
 
F

foo bar baz qux

Matt said:
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.
 

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,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top