in reply to Re: Zipcode Regex Help
in thread Zipcode Regex Help

I tried
my $zip_match = () = $card_zip =~ /(12345)\-?(6789)?/g;
and
$_ = $card_zip; my $zip_match = () = /(12345)\-?(6789)?/g;
Both Returned 0 on no match and 2 otherwise, even if only the first 5 digits match.

Replies are listed 'Best First'.
Re: Re: Re: Zipcode Regex Help
by PodMaster (Abbot) on Jun 25, 2003 at 21:50 UTC
    What did you expect it to return? Consider this
    my $bar = 'asdf asdf asdf-rdf'; my $mor = () = $bar =~ /asdf(?:-rdf)?/; my $gor = () = $bar =~ /asdf(?:-rdf)?/g; my @gor = $bar =~ /asdf(?:-rdf)?/g; warn scalar @gor; die "$mor and $gor = @gor"; __END__ 3 at - line 6. 1 and 3 = asdf asdf asdf-rdf at - line 7.
    update:
    It returns the number of times the entire pattern matched ( aka the number of total matches). .
    My apologies. What's returned are the values of the capture buffers (parens create a capture buffer). my $mor = () = $bar =~ /asdf(?:-rdf)?/; could be rewritten as my $gor = my @gor = $bar =~ /asdf(?:-rdf)?/g;

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

      For zipcode 12345, I would expect it return 1, since only (12345) matched, and not (6789), but I guess because of the '?' in (6789)?, it counts it as a match.