in reply to Problem with alternating regex?
The problem is that $1 is the first (and outermost) set of capturing parenthesis so you capture the id \d+ prefix along with the stuff you want. A clean fix is to make the id match optional:
use warnings; use strict; my $strs = <<STRS; set zone "VLAN" vrouter "trust-vr" set zone for "non-matched" stuff set zone id 100 "Internet_Only" quoted "stuff" without set zone STRS open my $fIn, '<', \$strs; while (defined(my $line = <$fIn>)) { next if $line !~ /^set\szone\s (?:id\s\d+\s)? "([^"]*)"/x; my $zone = $1; chomp $line; print "Config line=> $.; Value=> $line; zone=> $zone\n"; }
Prints:
Config line=> 1; Value=> set zone "VLAN" vrouter "trust-vr"; zon +e=> VLAN Config line=> 3; Value=> set zone id 100 "Internet_Only"; zone=> + Internet_Only
which retains a fairly specific match, but allows the variability you need. Notice that (?: ...) is used to provide grouping without capturing. Oh, and the x flag lets me use some white space in the regex so the moving parts are easier to see.
True laziness is hard work
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Problem with alternating regex?
by dwlepage (Novice) on Sep 11, 2012 at 05:16 UTC |
In Section
Seekers of Perl Wisdom