作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在尝试使用 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/
我是一名优秀的程序员,十分优秀!