Java 模式匹配只关闭单词

Java 模式匹配只关闭单词,java,regex,Java,Regex,我有一个包含书籍段落和短语的数据库,我试图从中生成HTML文件。为了简单起见,假设我想加粗一些短语 我有以下一句话: Following that mission I was sent on further missions. Most of them succeeded and we are living with the consequences today. They must remain secret. If the truth ever came out, then some of

我有一个包含书籍段落和短语的数据库,我试图从中生成HTML文件。为了简单起见,假设我想加粗一些短语

我有以下一句话:

Following that mission I was sent on further missions. Most of them succeeded and we are living with the consequences today. They must remain secret. If the truth ever came out, then some of the national leaders at the time would find themselves in the Hague on war crimes charges.
我想说一个大胆的使命词——很简单。我只用于模式任务词。 但有时短语的单词并不相邻。在示例句中,它是“被控” 所以我发现我可以在数据库中输入通配符,稍后我会在程序中替换它。所以在数据库中它是“按%收费” 此外,我不想将不是短语部分的单词加粗

所以输出应该是

<b>on</b> war crime <b>charges</b>
战争罪指控 我的Java代码如下所示(w.getWord()返回'on%charges'

String pat = w.getWord().replace("%", ".+?").trim();
Pattern p = Pattern.compile("(\\W)(" + pat + "){1}?(\\W)");
text = text.replaceFirst(p.pattern(), "$1<b>$2</b>$3");
String pat=w.getWord().replace(“%”,“+?”).trim();
模式p=Pattern.compile(“(\\W)(“+pat+”){1}?(\\W)”;
text=text.replaceFirst(p.pattern(),“$1$2$3”);
现在在这句话中,问题出现了,如果在这句话之前有另一个on,它会被加粗到很多,加粗的输出是:

Following that mission I was sent <b>on further missions. Most of them succeeded and we are living with the consequences today. They must remain secret. If the truth ever came out, then some of the national leaders at the time would find themselves in the Hague on war crimes charges</b>
在那次任务之后,我被派去执行进一步的任务。大部分任务都取得了成功,我们今天正在承受着后果。这些任务必须保密。如果真相大白,那么当时的一些国家领导人会发现自己因战争罪被关押在海牙

Nope可能重复。这是不同的。我不是解析HTML,而是生成它。请阅读问题所在。