Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Problem with alternating regex?

by GrandFather (Cardinal)
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"; }

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


Comment on Re: Problem with alternating regex?
Select or Download Code
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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://992909]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2014-10-22 00:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (112 votes), past polls