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.)


In reply to Regexp glitch while parsing record in CSV by greenhorn

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.