Phoenix.pm: Sorry for the dup

phaedrus at illogics.org phaedrus at illogics.org
Tue Nov 26 14:48:43 CST 2002


Ack, sorry for the duplicate. I thought my message was rejected again.
Too many email aliases ;)

> s{(\w+)\s*=?\s*(['"]?)(.*?)(?<!\\)\2}{$hash{$1} = $3;''}ge;
> foreach my $i (keys %hash) { $hash{$i} =~s/\\//g }

This prolly needs explained. 

Globally, we're matching a word, followed by any amount of space, an optional
equal sign, any amount of space, an optional quote character, anything at all*, 
and then whatever we matched for the quote character, which may be nothing.
However, the second time, we don't just match the quote character - we put
a conditional in front of it - (?<!\\) only lets the REGEX continue on matching
if the previous thing was NOT a \. We need a \ to escape our \. This is a
"zero width negitive look-behind" asserition. We require (assert) something: the it
isn't true (negitive) that previous thing (look-behind) is \.

* "Anything at all" is matched non-greedily, so it stops as soon as the next thing
in the regex would match.

After that, we just have to remove the now redundant \'s from our values and
we're good to go.

-scott




More information about the Phoenix-pm mailing list