in reply to Match on line, read backwards to opening xml tag then forward to closing tag

If you are interested in using an XML parser instead of a regex, XML::Twig can give you the whole element you are looking for:
use warnings; use strict; use XML::Twig; my $str = ' <DataEnd> <Dataentry> <Data>aaaaaa</Data> <Data>aaaaaa</Data> <Data>aaaaaa</Data> </Dataentry> <Dataentry> <Data>aaaaaa</Data> <Data>aaaaaa</Data> <Data>aaaaaa</Data> </Dataentry> <Dataentry> <Data>aaaaaa</Data> <Data>bbbbbb</Data> <Data>aaaaaa</Data> </Dataentry> <Dataentry> <Data>aaaaaa</Data> <Data>aaaaaa</Data> <Data>aaaaaa</Data> </Dataentry> </DataEnd> '; my $t = XML::Twig->new( twig_handlers => { Dataentry => \&dentry } ); $t->parse($str); sub dentry { my ($t, $ent) = @_; my $found = 0; for my $data ($ent->children('Data')) { if ($data->text() eq 'bbbbbb') { $found = 1; next; } } if ($found) { # do something } }
  • Comment on Re: Match on line, read backwards to opening xml tag then forward to closing tag
  • Download Code

Replies are listed 'Best First'.
Re^2: Match on line, read backwards to opening xml tag then forward to closing tag
by admiral_grinder (Pilgrim) on Nov 15, 2011 at 15:04 UTC
    I'll see your use of a XML parser, but raise you XML::LibXML. It is a pain to install on Windows systems but not impossible. I have used both alot and you can get significant speed increases with LibXML (talking seconds to nanoseconds)