in reply to Regex to match string with numbers with possible comma

This should do it: =~ /Altavista found ([\d,]+) results/

Replies are listed 'Best First'.
Re: Re: Regex to match string with numbers with possible comma
by Anonymous Monk on Mar 17, 2004 at 16:23 UTC
    I tried your regex but it's not pulling back a result. It comes back as "". Any idea why? The url I'm trying is http://www.altavista.com/web/results?q=url:www.tek-tips.com&kl=XX&search=Search.
    #!/usr/bin/perl use LWP::Simple; use strict; $|=1; my $url = "www.tek-tips.com"; my $altavista = "http://www.altavista.com/web/results?q=url:$url&kl=XX +&search=Search"; my $content = get("$altavista"); my @lines = split /\n/, $content; my $results; foreach (@lines) { $results = $1 if $_ =~ m/Altavista found ([\d,]+) results/; } print "searched: $altavista\n"; print "results: $results";

      The 'v' in AltaVista needs to be capitalized, and you should be good to go. (With the exception that the commas aren't stripped.) There's an example of how to do that in this thread by Anonymous Monk Chris.

      I don't know what you plan on doing, but I typically strip commas right away -- they're nothing but trouble.

        Okay, the capital V made it work.. That was a "DUH!" I should have caught. But I don't see why you said "but I typically strip out commas right away -- they're nothing but trouble". What harm could a comma possibly do when it's between quotes? It's interpreted as nothing more than text.

        Thanks for your help!