in reply to regexp over multiple lines
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: regexp over multiple lines
by liverpaul (Acolyte) on Aug 03, 2011 at 07:29 UTC | |
by Sinistral (Monsignor) on Aug 03, 2011 at 13:33 UTC | |
|
Re^2: regexp over multiple lines
by liverpaul (Acolyte) on Aug 03, 2011 at 07:56 UTC | |
by JavaFan (Canon) on Aug 03, 2011 at 08:13 UTC | |
by liverpaul (Acolyte) on Aug 03, 2011 at 08:27 UTC |