【发布时间】:2016-08-25 14:41:27
【问题描述】:
我有一个包含一些我想标记的单词的文本,并且要标记的单词包含在一个列表中。问题是其中一些单词是其他单词的子字符串,但我想标记列表中最长的识别字符串。
例如,如果我的文本是“foo and bar is different from foo bar”。我的列表包含“foo”、“bar”和“foo bar”,结果应该是“[tag]foo[/tag] 和 [tag]bar[/tag] 不同于 [tag]foo bar[/tag] 。”
String text = "foo and bar are different from foo bar.";
List<String> words = new ArrayList();
words.add("foo");
words.add("bar");
words.add("foo bar");
String tagged = someFunction(text, words);
someFunction 的代码应该是什么,使得字符串 taggedText 的值为 <tag>foo</tag> and <tag>bar</tag> are different from <tag>foo bar</tag>. ?
【问题讨论】:
-
按长度排序。
标签: java regex string substring