[tpm] I wish I was better at regex's
Shaun Fryer
sfryer at sourcery.ca
Wed Mar 9 11:29:04 PST 2011
It's very difficult to do without matching quotes, and hence key/value
pairs. For instance, what if the comment contains a quote, just as the
quotes may contain a semi-colon? In this case I've sidestepped the
problem of escaped quotes by using a word-boundary (\b) match inside
any matched quotes. the side effect of this, is that any spaces inside
the quotes will count...
good luck if you can do it without matching keys/values/quotes. I've
already got beers riding on the solution. ;)
--
Shaun Fryer
cell: 1-647-709-6509
voip: 1-647-723-2729
On Wed, Mar 9, 2011 at 2:25 PM, Fulko Hew <fulko.hew at gmail.com> wrote:
>
>
> On Wed, Mar 9, 2011 at 2:16 PM, Shaun Fryer <sfryer at sourcery.ca> wrote:
>>
>> my $strip = qr{;[^\;]+$};
>> while (<DATA>) {
>> chomp;
>> my ($key, $val) = split /=/;
>> my ($quote) = ($val =~ m{^(["'])}g);
>> if ($quote) {
>> ($val) = ($val =~ m{^($quote[^\b]+($quote))}g);
>> }
>> else {
>> $val =~ s/$strip//;
>> }
>> print $key, '=', $val, "\n";
>> }
>
> Closer, but it still assumes the key=value concept,
> the goal was 'any trailing comment' (not quoted) ie. regardless of the
> syntax before it
> as in the following additional test case:
>
> title ;comment
>
>
More information about the toronto-pm
mailing list