Jean Jean - 7 months ago 17
Perl Question

Capture in split

I do this:

my @words =split(/[~,;#&=\.\s\|\(\)\+\-\?\:]+/,$string);
my @processed_words = ();
foreach (@words) {push(@processed_words,process_word($_));}


Could someone suggest an easy way to compose
$string
back after processing while keeping the unprocessed parts intact?
Or is
split
not the way to go here?

Answer

If you put the delimiters in the regexp in a capture group, then split will include the delimiters in its result -- it will alternate between words and delimiters. You can then push the unprocessed delimiters and the processed words onto the result array.

my @words =split(/([~,;#&=\.\s\|\(\)\+\-\?\:]+)/,$string);
my @processed_words = ();
foreach (@words)  {
    if (/[~,;#&=\.\s\|\(\)\+\-\?\:]/) { // delimiter, just copy it
        push(@processed_words, $_)
    } else { // process the word
        push(@processed_words,process_word($_));
}