#!/usr/bin/perl # http://perlmonks.org/?node_id=1192280 use strict; use warnings; $_ = do { local $/; }; # find inner html sections my $nestcount = 1; while( s:( # starting html ((?!).)*? # no html or /html in the middle # ending /html ): NON_NESTED_HTML $nestcount :sx ) { my $nonnestedhtml = $1; print "NON_NESTED_HTML $nestcount\n\n$nonnestedhtml\n\n"; $nestcount++; # here you can parse an inner non-nested html section } print "this is what's left\n\n$_\n\n"; __DATA__

Hello

this is a test

this is a second test

some kind of wrapped footer

#### NON_NESTED_HTML 1

Hello

this is a test

this is a second test

NON_NESTED_HTML 2 NON_NESTED_HTML 1

some kind of wrapped footer

this is what's left NON_NESTED_HTML 2