Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
I am assuming you want to slurp the whole file into the variable $info. It might be easier to do this as follows:
{ local $/ = undef; $info = <DATA>; }
For more info on $\ look at perlvar

Apart from this, I have changed the regex a little bit to do more of what I think you want it to do. Here is the code.

use strict; use warnings; use Data::Dumper; my $info; { local $/ = undef; $info = <DATA>; } my %lines; while ($info =~ m/(\d+)\: (.+?)\n(?=\d)/gs) { $lines{$1} = $2; } print Dumper (%lines); __DATA__ 3: Tag <test> found Tag <test> found 5: Tag <test> found 7: Tag <test> found 14: Tag <test> found 16: Tag <test> found 18: Tag <test> found 21: Tag <test> found 25: Tag <test> found 27: Tag <test> found 29: Tag <test> found 32: Tag <test> found 34: Tag <test> found 49: Tag <test> found 80: Tag <test> found 98: Tag <test> found Tag <test> found
and here is the output:
$VAR1 = '29'; $VAR2 = 'Tag <test> found'; $VAR3 = '21'; $VAR4 = 'Tag <test> found'; $VAR5 = '7'; $VAR6 = 'Tag <test> found'; $VAR7 = '14'; $VAR8 = 'Tag <test> found'; $VAR9 = '80'; $VAR10 = 'Tag <test> found'; $VAR11 = '32'; $VAR12 = 'Tag <test> found'; $VAR13 = '16'; $VAR14 = 'Tag <test> found'; $VAR15 = '49'; $VAR16 = 'Tag <test> found'; $VAR17 = '25'; $VAR18 = 'Tag <test> found'; $VAR19 = '3'; $VAR20 = 'Tag <test> found Tag <test> found'; $VAR21 = '34'; $VAR22 = 'Tag <test> found'; $VAR23 = '18'; $VAR24 = 'Tag <test> found'; $VAR25 = '27'; $VAR26 = 'Tag <test> found'; $VAR27 = '5'; $VAR28 = 'Tag <test> found';
. The regex: m/(\d+)\: (.+?)\n(?=\d)/gs
looks for a number then lazily matches up to where the next thing is a new line, but only if the first thing after that new line is a digit.

Well, I think this is what you want.

UPDATE: I meant to have placed the Dumper(\%lines) as BrowserUK has done below, instead of just Dumper(%lines). Don't know what I was thinking.


In reply to Re: Matching over multiple lines in a scalar by Enlil
in thread Matching over multiple lines in a scalar by Rich36

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others exploiting the Monastery: (7)
    As of 2021-05-08 11:24 GMT
    Find Nodes?
      Voting Booth?
      Perl 7 will be out ...

      Results (96 votes). Check out past polls.