1. comma between two words like this - "Entries","Entry Time","Visit Length","Browser"

So this would be complicated by the fact that commas inside quoted text should not count, right? In other words, "Name, First","Name, Last","Birthday" should be split into "Name, First", then "Name, Last", and then "Birthday"?

I think that's starting to become a bit complicated for a single regex, though you could do it using match-time code evaluation and the \G assertion ("picking up where you left off"), perhaps, keeping track of whether you're in a quoted string or not as you go along.

A parser based on a formal grammar might be a better idea; take a look at Parse::RecDescent if you'd like to go that route.

Yet another option would be to use a loop and process the line in chunks: read from the beginning of (the current remainder of) the line up to the first comma or quote, keep track of whether you're in a quote right now etc., and use that information to either add the newly-read chunk to your current extracted column name, or increase your column counter and start a new column name with that chunk.

Since you want to learn how to do this on your own, I won't provide any code. :)

Now, all that said...

If commas are guaranteed to not appear in your quoted strings, everything becomes much easier, of course, \G alone should be enough to get the job done, though you could save yourself the trouble and simply split the line.

Hmm. I wonder if you could use split and then pull some clever tricks to glue the right bits together again in the general case, too. Well, you'll find out, if you decide to go down this route!

2. Match is multiple times a comma I tried many different regexes with no luck, please help!

I'm sorry, I don't know what you mean there.


In reply to Re: Match a comma between two words by AppleFritter
in thread Match a comma between two words by Stefany

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.