Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

reg exp matching problem

Status
Not open for further replies.

tecknick

Programmer
Aug 23, 2002
7
AU
I am trying to test some user input in a program.

example.....
Code:
    $input = '700.00,,'

    if ($input !~ /\d+\.\d+/)
        {
        print "invalid";
        }

I am only want input of the form 700.00 not
700.00,,

I could write .....

if( $input != '700.00')
{
print "invalid";
}

But I never know what the user will input.

How do I overcome this problem using reg exp's?

Thanks
 
this will match 700.00 or .5 etc..
the ^ and $ anchor the ends to the beginning and end of the string, so you're saying there can be nothing but digits on either side of the decimal point.
If you want it to match a number before the decimal point just change \d* back to \d+
Code:
if ($input !~ /^\d*\.\d+$/){
    print "invalid";
}else{
    print "valid";
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top