It's possibly that a capture group was missed in your explanation:

/((.*\.c\s)|(.*\.h\s)|(.*\.cpp\s))|(\s+(.*)\%\s+(of+)\s+\d+\s)|(\bNone +­\b)/g #01 2 3 4 5 6 7

If you lay that out using the /x modifier it becomes more obvious:

/ ( # 1 (.*\.c\s) # 2 | (.*\.h\s) # 3 | (.*\.cpp\s) # 4 ) | (\s+ # 5 (.*)\%\s+ # 6 (of+)\s+\d+\s # 7 ) | (\bNone­\b) # 8 /gx

My preference would be to first reduce the capturing to just those parts that are needed. For example, it's unlikely that one would want both "1" and "2", "3", and "4". Likewise, it's unlikely that someone would care about "5" while also caring about "6", and "7".

Second, resort to named captures: (?<somename>...). And third, to look at breaking it up into smaller problems with /g and \G

I think, in particular, that named captures and (?:...) grouping where capturing isn't needed would make this easier to use.


Dave


In reply to Re^2: how to extract string by possible groupings? by davido
in thread how to extract string by possible groupings? by adrive

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.