Lele
Lele

Reputation: 703

Java remove all Exclamation point

i've a simple question, i need to remove all exclamation point from an HTML string in Java. i've tried with

testo = testo.replaceAll("\\\\!", "! <br>");

and

regex = "\\s*\\b!\\b\\s*";
        testo = testo.replaceFirst(regex, "<br>");

and

 testo = testo.replaceAll("\\\\!", "! <br>");

But doesn't work. Can someone help me? another little question, i need to replace 1, 2 or 3 exclamation point with a single breakline thank's to all!

Upvotes: 0

Views: 3541

Answers (2)

Jerry
Jerry

Reputation: 71578

You don't have to escape the exclamation mark:

testo = testo.replaceAll("!{1,3}", "! <br>");

Should do.

{1,3} means 1 to 3 consecutive occurrences.

Upvotes: 2

anubhava
anubhava

Reputation: 785661

Why do you need regex for this? You can simply do String#replace

testo = testo.replace("!", "! <br>");

However to remove multiple exclamation marks use:

testo = testo.replaceAll("!+", "! <br>");

Upvotes: 2

Related Questions