in reply to Re^2: Likely trivial regex question
in thread Likely trivial regex question
That "matches" both strings, but doesn't capture the number that follows "vodka" in the first string. Sorry, but I can't explain why.my $re = qr/beer=(\d{2}).*(?:vodka=(\d{2}))?.*chips=(\d{3})/;
So I'd be inclined to take a different approach:
UPDATE: Here's a variant on that approach, which I think is closer to what your own snippet would actually do if it worked (in case that's really what you want):#!/usr/bin/perl use strict; use warnings; my @strs = ( "beer=10&otherstuff&vodka=20&otherstuff&chips=100", "beer=10&otherstuff&juice=20&otherstuff&chips=100" ); my @targets = qw/beer vodka chips/; for ( @strs ) { my @matched; for my $target ( @targets ) { push @matched, $1 if ( /$target=(\d+)/ ); } if ( @matched ) { print "matched: @matched\n"; } }
my @targets = qw/beer=(\d{2}) (vodka=(\d{2})) chips=(\d{3})/; for ( @strs ) { my @matched; for my $target ( @targets ) { push @matched, ( /$target/ ); } if ( @matched ) { print "matched: @matched\n"; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Likely trivial regex question
by moodywoody (Novice) on Nov 09, 2011 at 10:35 UTC | |
by ww (Archbishop) on Nov 09, 2011 at 12:38 UTC | |
by choroba (Cardinal) on Nov 09, 2011 at 11:00 UTC | |
|
Re^4: Likely trivial regex question
by choroba (Cardinal) on Nov 09, 2011 at 10:56 UTC |