3

I've got an ArrayList<String> named fields. I'm trying to parse the HTML in each String using the replaceAll function, but I get the feeling that I'm screwing up the regex String (I got the 2nd regex here to represent a generic html expression). Can anyone give me some tips on how to correct myself here?

for(int j = 0; j<fields.size(); j++)    
{
    String k = fields.get(j);
    k.replaceAll("<br>", "\n");
    k.replaceAll("<(\"[^\"]*\"|'[^']*'|[^'\">])*>", "");
    k.replaceAll("&lt;", "<");
    k.replaceAll("&gt;", ">");
    fields.set(j, k);
}
1
  • 2
    It would be far easier to use something like jsoup. Then you just need to do Jsoup.parse(str).text(); Commented Oct 5, 2012 at 21:43

1 Answer 1

4

Remember that strings are immutable, so you want to re-assign k each time you call replaceAll:

String k = fields.get(j);
k = k.replaceAll("<br>", "\n");
...
Sign up to request clarification or add additional context in comments.

2 Comments

facepalm You're absolutely correct, thanks. That fixed everything.
No, it didn't fix everything. You're still using regular expressions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.