|XP is just a number|
Regexp glitch while parsing record in CSVby greenhorn (Sexton)
|on Jul 17, 2000 at 04:21 UTC||Need Help??|
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:
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):
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.)