in reply to Regexp and metacharacters

Turning your code into one statement is such a weird requirement, but it's easy to do

my $fixed = $ptext =~ s/^\"//r =~ s/\"$//r =~ s/^\'//r =~ s/\'$//r =~ s/\'/\\\'/gr =~ s/\"/\\\"/gr =~ s/\{/\\\{/gr;

It's not how I'd do it, though.

Replies are listed 'Best First'.
Re^2: Regexp and metacharacters
by Largins (Acolyte) on Dec 27, 2011 at 03:32 UTC

    Ok, so how would you do it?

      I would go for readability instead.

      my $fixed = $ptext; $fixed =~ s/^['"]//; $fixed =~ s/['"]\z//; $fixed = quotemeta($fixed);

      That's probably useless since your list of meta characters differs from quotemeta's. If so, then you can use:

      my $fixed = $ptext; $fixed =~ s/^['"]//; $fixed =~ s/['"]\z//; $fixed =~ s/([\\'"...])/\\$1/g;