Reputation: 5609
I am trying to replace all the characters between an html font tag with an expression. I wrote a little test program but it is not working correcty. Here is my regular expression:
test.replaceAll("<font\b(.*)>", "Something");
This does not work.
Why?
Upvotes: 1
Views: 178
Reputation: 68152
You probably want two "\" before the "b":
test.replaceAll("<font\\b(.*)>", "Something");
You need this because the regular expression is a string and backslashes need to be escaped in strings.
To make it only match up to the first ">", do this:
test.replaceAll("<font\\b(.*?)>", "Something");
This makes the *
"lazy", so that it matches as little as possible rather than as much as possible.
However, it seems that it is better to write this particular expression as follows:
test.replaceAll("<font\\b([^>]*)>", "Something");
This has the same effect and avoids backtracking, which should improve performance.
Upvotes: 2
Reputation: 420990
Note that the *
operator is greedy, i.e.,
String test = "<font size=\"10\"><b>hello</b></font>";
System.out.println(test.replaceAll("<font\\b(.*)>", "Something"));
prints
Something
You may want to use [^>]*
test.replaceAll("<font\\b([^>]*)>", "Something")
or a reluctant quantifier, *?
:
test.replaceAll("<font\\b(.*?)>", "Something")
which both result in
Something<b>hello</b></font>
Upvotes: 5