这是将为您完成这项工作的 Java 代码:
String str =
"@jhon.p @[User:151|Jhon Carter] Jhon Carter @sunil.p @[User:102|Sunil Nadar] Sunil Nadar";
Matcher m = Pattern.compile("@\\[User:(\\d+)\\|([^]]+)\\]\\s+\\2").matcher(str);
StringBuffer buf = new StringBuffer();
while (m.find()) {
String link = "<a href=\"#/users/" + m.group(1) + "\">" + m.group(2) + "</a>";
m.appendReplacement(buf, link);
}
m.appendTail(buf);
System.out.printf("Converted String is: %s%n", buf);
输出:
Converted String is: @jhon.p <a href="#/users/151">Jhon Carter</a> @sunil.p <a href="#/users/102">Sunil Nadar</a>