Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

How can I match a multi-line regexp?

( #17947=categorized question: print w/ replies, xml ) Need Help??
Contributed by Anonymous Monk on Jun 13, 2000 at 22:34 UTC
Q&A  > regular expressions


Description:

How do I match something like:

BASKET

OPEC WEEKLY BASKET PRICE RISES TO 28.23 DOLLARS

that is - a single keyword, followed by 2 newlines, followed by a single line of several words terminated by another newline.

Answer: How can I match a multi-line regexp?
contributed by nuance

It's hard to be certain exactly what you're looking for, I'm going to assume that you are looking for a specific keyword and that the text following is fairly free form.

my $keyword = "BASKET"; $data =~ /^($keyword\n\n.+)$/m;

This looks for the keyword followed by two newlines, and whatever else is on that line. It will not match if the line following the two newlines is blank. The m at the end of the pattern specifies multi-line matching i.e. the $ will match before any newline, not just at the end of the string.

This answer assumes that $data contains all of the text you are trying to search, not just a single line.

Answer: How can I match a multi-line regexp?
contributed by athomason

You need to tell the regex engine to treat your scalar as a multiline string with the /m option; otherwise it won't attempt to match across newlines. For instance,

$_ = "bunch of stuff\nwith target\nstring inside"; print "Found" if /target\nstring/m;
Answer: How can I match a multi-line regexp?
contributed by wesal

To overcome the limitation with the quick in-place edit not working on matching the first-line you can remove the -p and do it yourself like:

perl -i.bak -e 'undef $/; $_=<>; s/line1\nline1/new content/g; print' files ..
Answer: How can I match a multi-line regexp?
contributed by markjugg

If you want to do a quick in-place edit of a bunch of files, you can use this syntax:

perl -pi.bak -e 'undef $/; s/line1\nline2/new content/' files ..

This will create ".bak" files for every file it operates on. NOTE that it won't find any matches on the first line. If someone knows another one-liner that also matches on the first line, I'm interested to see it. :)

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • 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?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others perusing the Monastery: (4)
    As of 2015-07-03 21:03 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (56 votes), past polls