Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

The while (<INF>) only reads one line at a time (where a "line" is defined by the input record separator, $/ which is a newline by default). Since you're only pulling in one line at a time, you'll never match across multiple lines. You need to reset the record separator. For example:

{ # Set the record separator to match the data. # In this example I have a blank line between # the lines I want to match, so set $/ to two # newlines. # Note that I'm setting $/ in its own data block (the empty # brackets) to localize the record separator change. $/ = "\n\n"; while (<DATA>) { if(/First line of text[\n]second line of text[\n]in the third +line I have (\d*\.\d*)/s) { print "Captured ($1)\n"; } } } __DATA__ First line of text second line of text in the third line I have 3.14159 Here is another line of text second line of text third line has 1.41421356 First line of text second line of text in the third line I have 1.73205081

Note that due to the regex, only the first and third numbers will be output.

You could also set the record separator to "First line of text", but keep in mind that you'll need to remove that bit from the regex:

$/ = "First line of text"; while (<DATA>) { if(/[\n]second line of text[\n]in the third line I have (\d*\. +\d*)/s) { print "Captured ($1)\n"; } }

Added: You could also undef the separator and match globally:

{ $/ = undef; my $data = <DATA>; my @array = $data =~ /First line of text[\n]second line of text[\n +]in the third line I have (\d*\.\d*)/g; for (@array) { print "Captured $_\n"; } }

In reply to Re: multi-line parsing by Nkuvu
in thread multi-line parsing by amit223

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 wandering the Monastery: (7)
    As of 2021-03-08 16:05 GMT
    Find Nodes?
      Voting Booth?
      My favorite kind of desktop background is:

      Results (126 votes). Check out past polls.