in reply to Re: Search question
in thread Search question

Thanks Dave... worked great... My next problem is to search the amount field for dollar amounts.... I need to be able to search for dollar amounts in ranges... ie ($1,000 or less) ($1,000 - $1,500) etc, etc.. here's my code for the dollar amount to search and it doesn't seem to work right..
<select name="searchprice" size="1" value="price"> <option value="\$" selected>Any Price <option value="<1000">Under \$1,000 <option value="1000-1500">\$1,000 - \$1,500 <option value="1501-2500">\$1,500 - \$2,500 <option value="2501-5000">\$2,500 - \$5,000 <option value="5001-10000">\$5,000 - \$10,000 <option value="10001-15000">\$10,000 - \$15,000 <option value="15001-999999">\$15,000 - UP </select></font></td></tr> if ($desc =~ /$form{'searchprice'}/) {print "$desc\n";}
My data looks like this:

bla bla bla bla $1,000
bla bla $12,500. bla bla
bla $5000 bla bla bla

sometimes it has a comma sometimes it doesn't...
sometimes it has a period directly after it and so on...

____SysAdm

Replies are listed 'Best First'.
Re: Search question : Price Regex
by dfog (Scribe) on Mar 11, 2001 at 22:42 UTC
    To match that I would do something like :
    my ($LowValue, $HighValue) = split("-", $form{'searchprice'}); #makes + the two values to compare $Price with if ($desc =~ m/\$(\d{1,3},?[\d{3},?]*\.?\d{0,2})/) { my ($Price) = $1; #sets $Price equal to the match in the regex $Price =~ s/,//g; #Strips out all commas if (($LowValue <= $Price) && ($Price <= $HighValue)) { print "$desc\n"; } } else { print "No price found in $desc\n"; }
    The regular expression is assuming that all of the prices in the description will begin with a $, and that they are properly formatted in terms of money (no misplaced commas and such). I believe this will do what you are looking for.

    Dave

      Dave... again you have enlightened me... I have incorported this little sniplet into the previous (4) finds before printing $desc... works great!!! Once again... a major thanks from me to you...

      The only line I don't quite understand is this one :

      if ($desc =~ m/\$(\d{1,3},?[\d{3},?]*\.?\d{0,2})/) {

      It's looking to match any decimal- but I don't get the (1,3) or the (3},? --- does it mean any decimal up to 3 places prior to the comma??? I tried using a 15001-999999 search criteria and it doesn't seem to like the 6 decimal places...

      Your help is tremendous... I owe you one....

      _______SysAdm

      2001-03-11 Edit by Corion : Removed PRE tags

        The code I used to create and use that regex (Commented) is
        #!/usr/bin/perl -w my %form = ( 'searchprice' => "15001-9999999", ); my @Tests = ('$1600', ' $1,300 ', ' $1,600,000.43 ', ' $1600. ', ' $16 +00.43 '); foreach my $desc (@Tests) { print "Testing $desc\n"; my ($LowValue, $HighValue) = split("-", $form{'searchprice'}); #s +plits two values to compare desc to if ($desc =~ m/ \$ #Matches the dollar sign ( #Begins gathering the digit grouping \d{1,3},? #Grabs between 1 and 3 digits. #There must be at least one and at mo +st #3 before the first comma. [\d{3},?]* #This grabs groups of 3 digits, #and the comma after them if it exist +s \.?\d{0,2} #This grabs the final decimal point a +nd #up to two places after it if they ex +ist )#Ends the digit grouping /x #tells it to ignore whitespace ) { my ($Price) = $1; #sets $Price equal to the match in the regex $Price =~ s/,//g; #Strips out commas print "TestPrice $Price\n"; if (($LowValue <= $Price) && ($Price <= $HighValue)) { print "$desc\n"; } } else { print "No price found in $desc\n"; } print "\n"; }
        The regex itself isn't quite as clean as it could be, but it works on the assumption that there is one and only one properly formatted price in each $desc.

        Dave