in reply to Re^6: Parsing file in Perl post processing
in thread Parsing file in Perl post processing

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
  • Comment on Re^7: Parsing file in Perl post processing