I don't want to delete the comments, I need to have them as-is in the final array. I just don't want to consider commas within the comments when I do the split.

Here's a typical input string (which I read in as a single line):

attr_def_OID OID, varname vartype1, /* Explanation */ varname vartype2, /* Explanation, details */ varname vartype3, /* Explanation, details, details */ varname vartype4 /* Explanation */
I want the resulting array to have 4 elements:
1: varname vartype1 2: /* Explanation */ varname vartype2 3: /* Explanation, details */ varname vartype3 4: /* Explanation, details, details */ varname vartype4 /* Explanation */
True, the comments don't get grouped with their intended lines, but that's not a problem for my application. I'm just doing some more text transformations, then joining them all back together. The important thing is that the comments come along for the ride.

This is what I'm using now to do the split, and it's working for the cases I've tried it with so far:

@elements = split(/,(?!(?:[^\/]|\/(?!\*))+\*\/)/, $str);

---
A fair fight is a sign of poor planning.


In reply to Re^2: Splitting outside comments by Sprad
in thread Splitting outside comments by Sprad

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.