http://www.perlmonks.org?node_id=22799

greenhorn has asked for the wisdom of the Perl Monks concerning the following question:

A regular expression I'm using hasn't been doing what I expect, and so far I'm baffled if I can see why.

The input file is a CSV file with 12 fields per record. The task is to get the contents of field 4. Field 1 always has content. Fields 2 and 3 might be blank. Field 4 always has content. Sometimes the field separators (commas) are preceded by or followed by spaces. I want to ensure these spaces are not included in the string extracted from field 4. One approach, as the file is being read line-by-line:

@record = split( /\s*,\s*/o, $_); $field4 = $record[3];

The regexp used with split does effectively filter out white space before and after the field separators. But despite the expected greater efficiency of "split", I found that in this particular script, a regular expression makes the script run noticeably faster (damned if I know why):

($s) = $_ =~ m/^\s*[^,]+,[^,]*,[^,]*,\s*([^,]+)\s*,/o; # "$s" now contains the contents of field #4

I would have thought the inclusion of "\s*" before and after the parens would ensure that 0+ leading and trailing spaces within the field would be removed.

But it isn't working. When field 4 has leading and trailing spaces in it, they are included in what's returned by the regular expression. Then I have to use:   $s =~ s/^\s+|\s+$//g;

to get rid of the spaces. There's something that's not working in the regexp--but I'm not seeing the error. If someone here does, I'd be much obliged for your advice about it.

(This to Ovid: This script is similar to--but simpler than--the one discussed in the other recent thread. Here again the regular expression seems to out-perform "split" every time. Dunno what to make of it.)