You need to go thru a good regex tutorial. Look in the Tutorials section on this site.

(.+?) The () says remember the part of the string that matches this. Matches what? The period is a wildcard, meaning match any letter, number, punctuation, whatever, EXCEPT a linefeed. The plus means match the previous thing 1 or more times. The question mark means don't be greedy - the matching should stop as soon as it can. This is important in this case because the . matches almost everything. .+ by itself would match an entire paragraph if you let it. So how does it know when to stop? The stuff following it tells it. \s means a whitespace character, like a space or tab. The asterisk means match it zero or more times. \s* means zero or more spaces. And $z, as we said before, matches something that looks like one of your keys.

So, taken all together, what does this mean? Let's assume $_ = 'THREAD_ID'. m/THREAD_ID:(.+?)\s*$z/. Look thru the string until you find THREAD_ID:. Starting with the very next letter, 1, match until you find either zero or more spaces followed by something that looks like a key. After the 8, there is a space and CDR_TYPE, which looks like a key (a combination of capital letters and underscores). So the match will stop right after the 8. The parentheses means 1bf1d698 is returned.

When I first wrote this, $z just matched a key, and this worked for everything except the last one in the list, because, being the last one, it isn't followed by something that looks like a key. So I added |$. $ matches the end of line, that linefeed character. | is a logical or. $z now means match something that looks like a key or the end of the line.

Dum Spiro Spero

In reply to Re^7: Parsing file in Perl post processing by GotToBTru
in thread Parsing file in Perl post processing by gbwien

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.