Jean Jean - 1 year ago 102
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
back after processing while keeping the unprocessed parts intact?
Or is
not the way to go here?

Answer Source

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download