Discussion Locked

Help with PerlUtil . . .

0 helpful
4 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I am trying to remove all HTML tags from a document using the Perl5Util class' substitute() method.
//doc is my HTML document
String new = "":
Perl5Util perlUtil = new Perl5Util();
new = perlUtil.substitute("s/<.*>//", doc);

The problem here, and I can't get around it, is that the regular expression removes everything from the document because the algorithm looks at the first '<' in and the last '>' in and removes everything between these characters. I want itto look at individual tags first. Can this be done?

4 replies

by kevin.cobb

As it happens, Perl is by default "greedy" which means it wants to match as much as it can. You can force it to be less greedy by adding a '?' in the regular expression. Here is an example:

$s1 = $s2 = "I am very very cold";
$s1 =~ s/ve.*y//; # I am cold
$s2 =~ s/ve.*?y //; # I am very cold

Notice how the second line matches every occurecy of "ve.*y". Where the last line matches only the first. So to correct my original PerlUtil statement:
new = perlUtil.substitute("s/<.*?>//", doc);

This removes all tags from an HTML document. Note: if a greater than or less than is used in the document as a mathematical character, things won't work as expected, which should be obvious.

by bayard-generationjava

From Jakarta Commons Util [in sandbox]'s XmlUtils class:

static public String removeXml(String str) {
int sz = str.length();
StringBuffer buffer = new StringBuffer(sz);
boolean inString = false;
boolean inTag = false;
for(int i=0; i') {
inTag = false;
continue;
}
if(!inTag) {
buffer.append(ch);
}
}
return buffer.toString();
}

[I'm sure the indentation will vanish, so apologies for ugly code]

by john.wilson

Kevin,
The perl HTML parser modules should do the job. HTML::Parser is the overall module but the HTML::TokeParser is a subclass to search for specific html tokens ( etc). You can get the basic description from the document at www.cpan.org and there are posted examples on the web. Search using "perl +html +parse +links" to find an example that will display all links in a document and the text they contain.
Once you have made the example work on a page, you can experiment with other tokens.
Have fun!
- john.wilson@johnmichaelwilson.com

This discussion is closed to new replies.