Yes and no. Technically I suppose you can, maybe doing something like this: use 5.010; use Data::Dumper;
$_ = "KEYWORD = VALUE MIN=2 MAX, = 12 WEIGHTED TOTAL= 20 WHAT =, 12 TEST = 1000"; our %results; m! (?&RECURSE) (?(DEFINE) (?<BAD_ASSIGNMENT> (?&BAD_LVSIDE)|(?&BAD_RVSIDE) ) (?<BAD_LVSIDE> (?&P_PLUS) = (?&P_STAR) ) (?<BAD_RVSIDE> (?&P_STAR) = (?&P_PLUS) ) (?<P_PLUS> \s*\p{P}+\s* ) (?<P_STAR> \s*\p{P}*\s* ) (?<SKIP_AND_RECURSE> (?&BAD_ASSIGNMENT) \w++ \s*+ (?&END) ) (?<END> (?&RECURSE) | \Z ) (?<KEYWORD_SEP> [\s\p{P}]*+ ) (?<RECURSE> (?<keyword> \w++ ) #Grab a keyword (?(?=(?&BAD_ASSIGNMENT)) #If it's followed by a malformed assignment, (?&SKIP_AND_RECURSE) #Grab the right side and skip forward. | #Otherwise, (?: \s* = \s* (?<value> \w+ ) )? #If there's an assignment, grab the value. (?{ $results{ $+{keyword} } = $+{value} // 1 #Get the results, }) ) (?&KEYWORD_SEP) #Eat up any whitespace/punctuation until the next keyword, (?&END) #And recurse. If we are at the end of the string, we are done. ) ) !x; say Dumper \%results; (Wow, gmail screwed up my indentation there. Here: http://ideone.com/vFSLc) But really you shouldn't; There is no need to do it in a single expression, other than causing your maintenance programmer some headache. And if doing it in one go is a requirement for whatever reason, using Regexp::Grammars[1] instead is probably a much better solution. Brian. [0] http://search.cpan.org/~dconway/Regexp-Grammars-1.012/lib/Regexp/Grammars.pm On Fri, Mar 18, 2011 at 7:24 PM, Chap Harrison <c...@pobox.com> wrote: > > This raises the question: is it possible, using a recursive regex and s/// > or m//, to - in one fell swoop - identify a correctly-formed string AND save > the key=value pairs into a hash? > > Eons ago, when writing a SNOBOL4 program to match an arbitrary algebraic > expression, I tried to make it evaluate the expression as it "unwound." I > never succeeded -- so it's become something of a holy grail for me. Not a > highly important one, though ;-) > > > > -- > To unsubscribe, e-mail: beginners-unsubscr...@perl.org > For additional commands, e-mail: beginners-h...@perl.org > http://learn.perl.org/ > > >