正则表达式,省略括号

时间:2013-09-20 07:50:06

标签: java regex

需要帮助找出我需要删除{{和}}之间所有数据的正则表达式吗?

以下是政变:

{{for|the American actor|Russ Conway (actor)}}
{{Use dmy dates|date=November 2012}}
{{Infobox musical artist <!-- See Wikipedia:WikiProject_Musicians -->
| birth_name          = Trevor Herbert Stanford
| 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              = 
}}

record|hits]].<ref name="British Hit Singles & Albums"/>

{{reflist}}

==External links==
*[http://www.russconway.co.uk/ Russ Conway]
*{{YouTube|TnIpQhDn4Zg|Russ Conway playing Side Saddle}}

{{Authority control|VIAF=41343596}}

<!-- Metadata: see [[Wikipedia:Persondata]] -->
{{Persondata
| NAME              =Conway, Russ
}}
{{DEFAULTSORT:Conway, Russ}}
[[Category:1925 births]]

下面是输出,其中所有大括号都与其中的文本一起被删除:

record|hits]].<ref name="British Hit Singles & Albums"/>
==External links==
*[http://www.russconway.co.uk/ Russ Conway]
*
<!-- Metadata: see [[Wikipedia:Persondata]] -->
[[Category:1925 births]]

P.S - 我省略了输出中的空格,我会照顾它。

2 个答案:

答案 0 :(得分:0)

string.replaceAll("\\{\\{[\\s\\S]*?\\}\\}","");

将产生:





record|hits]].<ref name="British Hit Singles & Albums"/>



==External links==
*[http://www.russconway.co.uk/ Russ Conway]
*



<!-- Metadata: see [[Wikipedia:Persondata]] -->


[[Category:1925 births]]

答案 1 :(得分:0)

这会处理嵌套的{{ }}

Matcher m=Pattern.compile("\\{[^{}]*\\}").matcher(input);
while(m.find())
{
    input=m.replaceAll("");
    m.reset(input);
}