leba-lev
leba-lev

Reputation: 2898

Regular expressions in java

String s= "(See <a href=\"/wiki/Grass_fed_beef\" title=\"Grass fed beef\" " +
          "class=\"mw-redirect\">grass fed beef.) They have been used for " +
          "<a href=\"/wiki/Paper\" title=\"Paper\">paper-making since " +
          "2400 BC or before.";

In the string above I have inter-mixed html with text.

Well the requirement is that the output looks like:-

They have been used for paper-making since 2400 BC or before.

Could some one help me with a generic regular expression that would produce the desired output from the given input?

Thanks in advance!

Upvotes: 0

Views: 132

Answers (2)

cHao
cHao

Reputation: 86506

The following expression:

\([^)]*?\)|<[a-zA-Z/][^>]*?>

will match anything that looks like an HTML tag and any parenthesized text. Replace said text with "", and there ya go.

Note: If you try to match any string that has script tags in it, or "HTML" where the author didn't bother to escape < and > when they weren't used as tag delimiters), or a ( without a ), things will probably not work as you'd hoped.

Upvotes: 1

jjnguy
jjnguy

Reputation: 138864

https://stackoverflow.com/questions/1732348#1732454

You have been warned.

Upvotes: 1

Related Questions