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

Hi,
I also believe that it is best to use a proper XML parser if you can, but I have encountered similar problems with non XML data, and then you do need to use regexes.
I think this is a nice workaround that will solve your problem (I hope)
use strict; use warnings; { my ($input_file) = @ARGV; open XML, $input_file or die; my $xml_data; ##Read the XML into xml_data while (my $line = <XML>){ $xml_data .= $line; } ##Parse xml_data while ($xml_data =~ /<Dataentry>(.*?)<\/Dataentry>/s){ ##Keep xml block to print if needed my $xml_block_to_print = $1; my $xml_block_to_parse = $xml_block_to_print; $xml_data = $'; ##Parse the xml block, and if we have a match, print! while ($xml_block_to_parse =~ /<Data>(.*?)<\/Data>/){ my $data = $1; $xml_block_to_parse = $'; if ($data eq 'bbbbbb'){ print "$xml_block_to_print\n"; last; } } } }
Please let me know if this works for you
mrguy

"Unix is user friendly - it's just a bit more choosy about who it's friends are."
  • Comment on Re: Match on line, read backwards to opening xml tag then forward to closing tag
  • Download Code