我的文字看起来像这样
| birth_date = {{birth date|1925|09|2|df=y}}
| birth_place = [[Bristol]], [[England]], UK
| death_date = {{death date and age|2000|11|16|1925|09|02|df=y}}
| death_place = [[Eastbourne]], [[Sussex]], England, UK
| origin =
| instrument = [[Piano]]
| genre =
| occupation = [[Musician]]
我想获取 [[ ]] 中的所有内容。我尝试使用 replace all 来替换不在 [[ ]] 内的所有内容,然后使用 split by new line 来获取带有 [[ ]] 的文本列表。
input = input.replaceAll("^[\\[\\[(.+)\\]\\]]", "");
要求的输出:
[[Bristol]]
[[England]]
[[Eastbourne]]
[[Sussex]]
[[Piano]]
[[Musician]]
但这并没有给出所需的输出。我在这里错过了什么?有成千上万的文档,这是获取它的最快方法吗?如果不是,请告诉我获得所需输出的最佳方式。
最佳答案
你需要匹配它而不是替换
Matcher m=Pattern.compile("\\[\\[\\w+\\]\\]").matcher(input);
while(m.find())
{
m.group();//result
}
关于Java正则表达式全部替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19186093/