Looks to me like your regexp is saying:
... \s*([^,]+)\s*,... Find some whitespace, or perhaps none at all. Find anything that is not a comma, at least once, which *includes* whitespace, then... Find some more whitespace, perhaps none Find a comma

Since perl is greedy, it takes the whitespace into the parens, and leaves nothing for the \s* afterwards to match.

I came up with this:

m#(?:[^,]*,\s*){3}(.*?)\s*,#
which seems to do the trick. Breaking it down:
(?: ## Group, but do not store it into $1 [^,]* ## Anything that is not a comma , ## Followed by a comma \s* ## Followed by possible whitespace ){3} ## Find three of these (the first three) (.*?) ## Match any character, but don't be so greedy about it \s* ## Possible whitespace , ## Stops at first comma, because we are not being greedy

There is probably a better way to write it, but I'm tired and this seems to work.


In reply to Re: Regexp glitch while parsing record in CSV by turnstep
in thread 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.