我正在尝试用表达式替换 html 字体标签之间的所有字符。我写了一个小测试程序,但它不能正常工作。这是我的正则表达式:
test.replaceAll("<font\b(.*)>", "Something");
这行不通。
为什么?
最佳答案
请注意,*
运算符是 greedy ,即,
String test = "<font size=\"10\"><b>hello</b></font>";
System.out.println(test.replaceAll("<font\\b(.*)>", "Something"));
打印
Something
你可能想使用[^>]*
test.replaceAll("<font\\b([^>]*)>", "Something")
或不情愿的量词,*?
:
test.replaceAll("<font\\b(.*?)>", "Something")
两者都会导致
Something<b>hello</b></font>
关于java正则表达式帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5669710/