Hello Monks, I'm just learning perl and regular expressions. Hopefully someone can help me out. I have a multi-line string that is semi-colon terminated. For example:
$_ = "10001 LONG RECORD\n BEGIN RECORD A, CODE B, TABLE C END \n NEXT\n STANDARD DATA 1 BEGIN CODE A, RECORD B END NEXT\n SHORT RECORD BEGIN TABLE B END NEXT\n STANDARD DATA 2 BEGIN CODE C, RECORD D, FILE END;";
I need to extract the data between each /BEGIN/ and /END/ from the string. The amount of sub-data records are variable but always given by a NEXT.

I'm thinking of writing a loop to match each group of expressions. But how do I count the amount of NEXT's in the string? Also how do I get the next set of /BEGIN/ /END/ expressions?

Final Regex question: I have the following in $1 = "CODE C, RECORD D, FILE";

How do I separate the 1st item from the the second, between comas. I.E. I want to have "CODE RECORD FILE" in one Array and "C D" in another. I thought the following would work:

(@recordSource, @recordType) = split(/\s/, split(/,/, $1));

But I only get a number in @recordSource.

Any tips or suggestions would be appreciated! Maybe I need a different approach?


In reply to Multiple RegEx Matches in a single string by Anonymous Monk

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.