Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

How can I match a multi-line regexp?

by Anonymous Monk
on Jun 13, 2000 at 22:34 UTC ( [id://17947]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question: (regular expressions)

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.

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: How can I match a multi-line regexp?
by nuance (Hermit) on Jun 14, 2000 at 02:49 UTC

    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.

Re: How can I match a multi-line regexp?
by athomason (Curate) on Jun 14, 2000 at 02:35 UTC
    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;
Re: How can I match a multi-line regexp?
by wesal (Initiate) on Aug 11, 2010 at 20:50 UTC
    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 ..
    
Re: How can I match a multi-line regexp?
by markjugg (Curate) on May 30, 2002 at 03:08 UTC
    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. :)
      what is the effect of undef $/; ?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://17947]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-03-28 20:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found