Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Re: Problem with alternating regex?

by GrandFather (Sage)
on Sep 11, 2012 at 03:12 UTC ( #992909=note: print w/replies, xml ) Need Help??

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"; }


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

    Thanks everyone. This has been very helpful!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://992909]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2018-06-22 11:38 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (124 votes). Check out past polls.