in reply to Parsing nested HTML with just regex
#!/usr/bin/perl use strict; use warnings; $_ = <<'--'; <div id = "foo"> Foo text <div id = "iwant"> Text text. <div id = "insideiwant"> Bla </div> <div id = "alsoinsideiwant"> Bla bla <em>Bla</em>! <div id = "innerinnerdiv"> Inner! </div> </div> </div> </div> -- my $div; $div = qr {<div \s+ (?:id \s* = \s* (?: "[^"]*" | '[^']*' | [-.\d]+))? \s* > (?: (?>[^<]+) | <(?!/?div) | (??{$div}) ) * </div>}ix; my $iwant = qr {<div \s+ id \s* = \s* (?: "iwant" | 'iwant' | iwant) \s* > (?: (?>[^<]+) | <(?!/?div) | (??{$div}) ) * </div>}ix; print $&, "\n" if /$iwant/; # Don't try to be the smartass # to point out potential issues # about $&. They are irrelevant # here. __END__ <div id = "iwant"> Text text. <div id = "insideiwant"> Bla </div> <div id = "alsoinsideiwant"> Bla bla <em>Bla</em>! <div id = "innerinnerdiv"> Inner! </div> </div> </div>
Abigail
|
|---|