Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^3: Problem with alternating regex?

by Athanasius (Monsignor)
on Sep 11, 2012 at 03:19 UTC ( #992910=note: print w/ replies, xml ) Need Help??


in reply to Re^2: Problem with alternating regex?
in thread Problem with alternating regex?

Greediness is not an issue for the OP because using a character class ([^"]*) constrains the match in any case.

Good point.

The double quote pairing is in fact correct
# /^set\szone\s("([^"]*)"|id\s\d+\s"([^"]*)")/ # ^ ^ ^ ^

Yes, it is actually the first format which has the right parenthesis in the wrong place. [Update 1: Apparently, I need better glasses! Sorry for the noise here.] But, as you say, that wasn’t the OP’s problem either.

So, try again (with thanks to Anonymous Monk, below):

if ($line =~ /^set\szone\s("[^"]*")|id\s\d+\s("[^"]*")/) { my $zone = $1 // $2; print "Config line=> $lineCount; Value=> $line; zone=> $zo +ne\n"; }

Hope that actually does help!

Update 2:
Your simplification is at the cost of matching almost anything.

Well, that’s a bit of an overstatement. Here is the OP’s original requirement:

Basically I want to match the first value found between double quotes although there are two different formats for the lines.

My first solution does match “the first value found between double quotes.” But, as you point out, it doesn’t take account of the 2 specific formats. My second solution will match only on one or other of these formats.

Athanasius <°(((><contra mundum


Comment on Re^3: Problem with alternating regex?
Select or Download Code
Re^4: Problem with alternating regex?
by GrandFather (Cardinal) on Sep 11, 2012 at 04:21 UTC
    The double quote pairing is in fact correct
    # /^set\szone\s("([^"]*)"|id\s\d+\s"([^"]*)")/ # ^ ^ ^ ^

    Yes, it is actually the first format which has the right parenthesis in the wrong place. But, as you say, that wasn’t the OP’s problem either.

    No. The OP's parenthesis matching really is correct - all of it. The parenthesis pairing is:

    # v v # /^set\szone\s("([^"]*)"|id\s\d+\s"([^"]*)")/ # ^ ^ ^ ^

    The OP's issue is that the parenthesis marked with 'v' are the $1 capture group. The two sets of parenthesis marked with ^ are what the OP hoped were $1, but really they are redundant grouping parenthesis.

    True laziness is hard work

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2014-07-30 22:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (241 votes), past polls