Process quoted text differently

/ Published in: Perl
Save to your folder(s)

This Perl snippet shows how to separate quoted parts from a text in order to process quoted and unquoted parts separately. For example, you could expand variables or wildcards only in the unquoted part. Then the different processed parts are put together again.

That a marker character "\x01" is used to mark the position of the quoted passages is a bit ugly, but on the other hand the single line of code putting the text together again is very elegant and shows off what Perl can do!

Copy this code and paste it in your HTML
  1. my @quoted;
  2. while( $text =~ s/('[^']*'|"[^"]*")/\x01/ ) {
  3. my $q= $1;
  4. # process quoted passages
  5. # ...
  6. push @quoted, $q;
  7. }
  8. # process unquoted $text
  9. # ...
  10. $text =~ s/\x01/shift @quoted/eg;

Report this snippet


RSS Icon Subscribe to comments

You need to login to post a comment.