Reputation: 1659
I currently have a perl script that imports HTML and converts it to plain text. I am using HTML::TagFilter to remove all the HTML tags and it is working almost perfectly except we've run into one issue. When the HTML contains non-stand HTML tags such as the "caption" in the example input below those tags aren't being removed:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam pulvinar, odio ut gravida fringilla, tellus mi ultrices felis, quis porta lacus sem ut lacus. Vestibulum massa justo, tristique id aliquet in, dapibus eu leo. Nam sapien risus, dictum et porttitor quis, egestas quis dui. Ut nec nisl felis. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.
[caption id="sample-id" align="sample-align" width="225" caption="Sample Caption"]<a href="http://www.domain.com/image.jpg"><img class="sample-image-class" title="Sample Title" src="http://www.domain.com/image.jpg" alt="Sample Alt" width="225" height="300" /></a>[/caption]
In hac habitasse platea dictumst. Duis imperdiet bibendum dolor ut ullamcorper. Suspendisse dui erat, facilisis sed aliquet non, elementum eu urna. Donec non nisi vel augue gravida imperdiet sed id tortor. Maecenas ullamcorper velit non dui imperdiet hendrerit.
What I need help with is a simple Perl regex to remove this content completely. I've tried a bunch of different approaches but nothing seems to be working. What I'm looking for is something like the following which would find and remove all occurrences of non-standard HTML tags using brackets []:
$text =~ s/[(\w)+](.*)[\/(\w)+]//g;
I'm hoping it is a simple exercise for someone who is better at regex than I am.
Thanks in advance for your help!
Upvotes: 0
Views: 371
Reputation: 442
You could use regex to remove only what's in the square brackets and trust HTML::TagFilter to remove the others.
$text =~ s! #Start match pattern (used exclamation mark instead of / for readability)
\[ #Left square bracket
[^\]]*? #Followed by any character(s) which are not ]. ? means lazy match
\] #Right square bracket
!!gx; #Replace with nothing, globally, allow comments and whitespace
Upvotes: 0
Reputation: 46487
You have to escape brackets because they have special meaning within regular expressions. Assuming that all attributes will be double-quoted with no double quotes inside them, the following should work.
$text =~ s/\[\/?\w+(\s+\w+="[^"]*")*\s*\/?\s*\]//g;
Upvotes: 2