Friday, December 31, 2010

Remove tag using lookahead assertion

use strict;
use warnings;

undef $/;

my $file = <DATA>;

while($file =~ s#<([^>]+)\s*([^>]+)?>((?!<.*?>).*?)<\/\1>##) {
    print "$3\n";
}


__END__

(?=pattern)
A zero-width positive lookahead assertion. For example, /\w+(?=\t)/ matches a word followed by a tab, without including the tab in $&.
(?!pattern)
A zero-width negative lookahead assertion. For example /foo(?!bar)/ matches any occurrence of ``foo'' that isn't followed by ``bar''. Note however that lookahead and lookbehind are NOT the same thing. You cannot use this for lookbehind.

No comments: