Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Match on line, read backwards to opening xml tag then forward to closing tag

by mrguy123 (Hermit)
on Nov 15, 2011 at 07:44 UTC ( [id://938097]=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2024-04-24 02:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found