Return to Snippet

Revision: 57964
at June 18, 2012 05:00 by deepsoul

Initial Code
my @quoted;
while( $text =~ s/('[^']*'|"[^"]*")/\x01/ ) {
    my $q= $1;
    # process quoted passages
    # ...
    push @quoted, $q;
# process unquoted $text
# ...
$text =~ s/\x01/shift @quoted/eg;

Initial URL

Initial Description
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!

Initial Title
Process quoted text differently

Initial Tags

Initial Language