我有一个字符串,
string1 = "Sri Lanka National Chess Championship this year and represented Sri Lanka at represented Sri Lanka Universities at the World University Chess Championships."
我还有另一个名为“string2”的字符串,它只有<NOUN> and </NOUN>
用空格分隔的“”标签包围的字符串。
string2 = "<NOUN>Sri Lanka National Chess Championship</NOUN> <NOUN>Sri Lanka</NOUN> <NOUN>Sri Lanka</NOUN> <NOUN>World University Chess</NOUN>"
请注意,第二个字符串可以有任何名词标记词(基于 'string1',例如:如果 string1 有 3 个名词,则 string2 将有相同的 3 个名词被名词标签包围)
我想将标签添加到 'string1' 和使 string1 如下,
string1 = "<NOUN>Sri Lanka National Chess Championship</NOUN> this year and represented <NOUN>Sri Lanka</NOUN> at represented <NOUN>Sri Lanka</NOUN> Universities at the <NOUN>World University Chess</NOUN> Championships."
我使用以下代码来执行此操作,
Pattern p = Pattern.compile("<NOUN>(.*?)</NOUN>");
Matcher m = p.matcher(string2);
while(m.find()) {
string1= string1.replaceAll(m.group(1),m.group(0));
}
但它给了我以下输出,
<NOUN><NOUN><NOUN>Sri Lanka</NOUN></NOUN> National Chess Championship</NOUN> this year and represented <NOUN><NOUN>Sri Lanka</NOUN></NOUN> at represented <NOUN><NOUN>Sri Lanka</NOUN></NOUN> Universities at the <NOUN>World University Chess</NOUN> Championships.
谁能告诉我如何正确地做到这一点?
或者请告诉我如何从给定的输出中获得所需的输出?