Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

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

Comment on Re: Problem with alternating regex?
Select or Download Code
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 the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2015-11-29 19:47 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (753 votes), past polls