Yes, it's possible. First you have to slurp in your input (ie, don't read line by line). Next, you need to set the /s modifier, and possibly /m for your regexp. /s tells the RE engine that '.' should match any character including a newline. /m tells the RE engine that $ and ^ should match at the beginning and ending of lines rather than beginning and ending of the string (that's what \A and \Z are for).

Also keep in mind that quantifiers such as * and + are greedy, so .* probably won't do what you want it to do when you hand it the following:

<tag>asdf</tag><tag>ghjkl</tag>

Unless what you want is for it to be greedy.

my $string = "<tag>asdf</tag><tag>ghjkl</tag>"; if( $string =~ m{<tag>(.*)</tag>} ) { print $1, "\n"; } __END__ asdf</tag><tag>ghjkl

Woops!

Now introducing perlre! :)

Expect some replies telling you to use a proper XML parser, such as XML::Twig, XML::Parser, XML::LibXML, XML::Simple, etc. And they're right. Better to let a well tested solution do the work for you.


Dave


In reply to Re: regexp over multiple lines by davido
in thread regexp over multiple lines by liverpaul

Title:
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, details, 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, summary, 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.