1

I would like to remove all the occurrences of angle brackets and the text in between, from my string. So given the following:

<a target="_blank" href="http://en.wikipedia.org/wiki/Grand_Theft_Auto_%28series%29">Grand Theft Auto</a> is a video game series created and primarily developed by Scottish developer Rockstar North, published in 1998.

I need this:

Grand Theft Auto is a video game series created and primarily developed by Scottish developer Rockstar North, published in 1998.

I have tried using the following, which doesn't seems to change the original string at all:

string.replaceAll("<.*?>","");
OFR
  • 90
  • 1
  • 1
  • 12

3 Answers3

2

Java strings are immutable, and don't change by themselves. You need to change them. Change this:

string.replaceAll("<.*?>","");

To this:

string = string.replaceAll("<.*?>","");
dryairship
  • 6,022
  • 4
  • 28
  • 54
0

Try

String str = string.replaceAll("\\<.*?\\>", "");

Edit: Correction made @John Hascall comment.

anaxin
  • 710
  • 2
  • 7
  • 16
  • 1
    This would be a better answer if you explained *why* that regex. (And BTW, I think your regex is wrong.) Consider an input: `Hello

    blahbleahblurgh

    World` -- This is why the original RE had the non-greedy qualifier `?`
    – John Hascall Feb 17 '16 at 16:58
0
String tmp = yourString.replaceAll("<a.*?>", "");
String finalString = tmp.replaceAll("</a>,"");
System.out.print(finalString);
Eray Balkanli
  • 7,752
  • 11
  • 48
  • 82