Posted By

deepsoul on 06/18/12


text parsing quoted

Versions (?)

Process quoted text differently

 / Published in: Perl

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!

  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  

You need to login to post a comment.