I'm trying to add hyperlink on stock information using JAVA matcher.
For example, this string will be changed
How do you think about Samsung and LG? I think Samsung is good.
to
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.
But, result was not I expected. :( It was only added 005930.
Here is output.
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.
Here is my code snippets. What did I wrong?
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);