#!/usr/bin/env perl use strict; use warnings; my $re = qr{ set \s zone \s (?> id \s \d+ \s | ) \" ( [^"]+ ) }x; my $out_format = "Config line=> %s; Value=> %s; zone=> %s\n"; while () { next unless /$re/; chomp; printf $out_format => $., $_, $1; } __DATA__ set zone "VLAN" vrouter "trust-vr" set zone id 100 "Internet_Only" #### $ pm_pref_quote_regex.pl Config line=> 1; Value=> set zone "VLAN" vrouter "trust-vr"; zone=> VLAN Config line=> 2; Value=> set zone id 100 "Internet_Only"; zone=> Internet_Only #### __DATA__ set zone "VLAN" vrouter "trust-vr" set zone id 100 "Internet_Only" blah blah blah set zone "extra" whatever blah blah blah "set zone id 12345 "extra2_a" something "extra2_b" #### $ pm_pref_quote_regex.pl Config line=> 1; Value=> set zone "VLAN" vrouter "trust-vr"; zone=> VLAN Config line=> 2; Value=> set zone id 100 "Internet_Only"; zone=> Internet_Only Config line=> 4; Value=> blah blah set zone "extra" whatever; zone=> extra Config line=> 5; Value=> blah blah blah "set zone id 12345 "extra2_a" something "extra2_b"; zone=> extra2_a #### $ perl -MYAPE::Regex::Explain -e ' my $re = q{^set\szone\s("([^"]*)"|id\s\d+\s"([^"]*)")}; print YAPE::Regex::Explain->new($re)->explain; ' The regular expression: (?-imsx:^set\szone\s("([^"]*)"|id\s\d+\s"([^"]*)")) matches as follows: NODE EXPLANATION ---------------------------------------------------------------------- (?-imsx: group, but do not capture (case-sensitive) (with ^ and $ matching normally) (with . not matching \n) (matching whitespace and # normally): ---------------------------------------------------------------------- ^ the beginning of the string ---------------------------------------------------------------------- set 'set' ---------------------------------------------------------------------- \s whitespace (\n, \r, \t, \f, and " ") ---------------------------------------------------------------------- zone 'zone' ---------------------------------------------------------------------- \s whitespace (\n, \r, \t, \f, and " ") ---------------------------------------------------------------------- ( group and capture to \1: ---------------------------------------------------------------------- " '"' ---------------------------------------------------------------------- ( group and capture to \2: ---------------------------------------------------------------------- [^"]* any character except: '"' (0 or more times (matching the most amount possible)) ---------------------------------------------------------------------- ) end of \2 ---------------------------------------------------------------------- " '"' ---------------------------------------------------------------------- | OR ---------------------------------------------------------------------- id 'id' ---------------------------------------------------------------------- \s whitespace (\n, \r, \t, \f, and " ") ---------------------------------------------------------------------- \d+ digits (0-9) (1 or more times (matching the most amount possible)) ---------------------------------------------------------------------- \s whitespace (\n, \r, \t, \f, and " ") ---------------------------------------------------------------------- " '"' ---------------------------------------------------------------------- ( group and capture to \3: ---------------------------------------------------------------------- [^"]* any character except: '"' (0 or more times (matching the most amount possible)) ---------------------------------------------------------------------- ) end of \3 ---------------------------------------------------------------------- " '"' ---------------------------------------------------------------------- ) end of \1 ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------