ウィキテキストのハイパーリンクには、次の 2 種類があります。
[[stack]]
[[heap (memory region)|heap]]
ハイパーリンクを削除したいが、テキストは保持したい:
stack
heap
現在、2 つの異なる正規表現を使用して、2 つのフェーズを実行しています。
public class LinkRemover
{
private static final Pattern
renamingLinks = Pattern.compile("\\[\\[[^\\]]+?\\|(.+?)\\]\\]");
private static final Pattern
simpleLinks = Pattern.compile("\\[\\[(.+?)\\]\\]");
public static String removeLinks(String input)
{
String temp = renamingLinks.matcher(input).replaceAll("$1");
return simpleLinks.matcher(temp).replaceAll("$1");
}
}
2 つの正規表現を 1 つに「融合」して同じ結果を得る方法はありますか?
提案されたソリューションの正しさを確認したい場合は、簡単なテスト クラスを次に示します。
public class LinkRemoverTest
{
@Test
public void test()
{
String input = "A sheep's [[wool]] is the most widely used animal fiber, and is usually harvested by [[Sheep shearing|shearing]].";
String expected = "A sheep's wool is the most widely used animal fiber, and is usually harvested by shearing.";
String output = LinkRemover.removeLinks(input);
assertEquals(expected, output);
}
}