gpt4 book ai didi

java - 用java匹配器替换所有

转载 作者:搜寻专家 更新时间:2023-11-01 03:18:43 25 4
gpt4 key购买 nike

我正在尝试使用 JAVA 匹配器在股票信息上添加超链接。

比如这个字符串会被改变

How do you think about Samsung and LG? I think Samsung is good.

How do you think about <a href="" kospi-code="005930">Samsung</a> and <a href="" kospi-code="003550">LG</a>? I think <a href="" kospi-code="005930">Samsung</a> is good.

但是,结果出乎我的意料。 :(只加了005930。

这是输出。

How do you think about <a href="" kospi-code="005930">Samsung</a> and <a href="" kospi-code="005930">LG</a>? I think <a href="" kospi-code="005930">Samsung</a> is good.

这是我的代码片段。我做错了什么?

String multipleStocks = "How do you think about Samsung and LG? I think Samsung is good.";
Pattern p = Pattern.compile("Hansum|LG|Samsung");
Matcher m = p.matcher(multipleStocks);

HashMap<String, String> stocks = new HashMap<String, String>();

stocks.put("Hansum", "020000");
stocks.put("Samsung", "005930");
stocks.put("LG", "003550");
String ts = null;

while(m.find()){
System.out.println(m.group());
ts = m.replaceAll("<a "+stocks.get(m.group(0))+">$0</a>");
}
System.out.println(ts);

最佳答案

试试这个代替你的循环。

for (String key : stocks.keySet()) {
multipleStocks=multipleStocks.replaceAll(key, "<a "+stocks.get(key)+">$0</a>");
}

System.out.println(multipleStocks);

关于java - 用java匹配器替换所有,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38432849/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com