2013-10-04 36 views
1

我的文字看起來像這樣Java的正則表達式替換所有

| 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替換不在[[]]內部的所有內容,然後使用新行分割來獲取[[]]的文本列表。

input = input.replaceAll("^[\\[\\[(.+)\\]\\]]", ""); 

需要的輸出:

[[Bristol]] 
[[England]] 
[[Eastbourne]] 
[[Sussex]] 
[[Piano]] 
[[Musician]] 

但是,這是不是給所需的輸出。我在這裏錯過了什麼?有成千上萬的文件,這是獲得它的最快方法嗎?如果不是,請告訴我獲得所需輸出的最佳方式。

+0

除了其它問題,請注意,'(+)'是「貪婪」量詞將抓住儘可能多的字符因爲它可以在'[['和']]'之間,這意味着'birth_place'你會得到''Bristol]],[[英格蘭'''作爲其中一場比賽。在'。+'之後加上'?',就像在falsetru的答案中一樣,阻止了這一點。 – ajb

回答

6

你需要匹配它不能代替

Matcher m=Pattern.compile("\\[\\[\\w+\\]\\]").matcher(input); 
while(m.find()) 
{ 
    m.group();//result 
} 
+0

@ ppeterka66是的,它會.. – Anirudha

+0

對不起,在我嘗試自己之前,我已經夠啞了,要問:) – ppeterka

2

使用Matcher.find。例如:

import java.util.regex.*; 

... 

String text = 
    "| birth_date   = {{birth date|1925|09|2|df=y}}\n" + 
    "| birth_place   = [[Bristol]], [[England]], UK\n" + 
    "| death_date   = {{death date and age|2000|11|16|1925|09|02|df=y}}\n" + 
    "| death_place   = [[Eastbourne]], [[Sussex]], England, UK\n" + 
    "| origin    = \n" + 
    "| instrument   = [[Piano]]\n" + 
    "| genre    = \n" + 
    "| occupation   = [[Musician]]\n"; 
Pattern pattern = Pattern.compile("\\[\\[.+?\\]\\]"); 
Matcher matcher = pattern.matcher(text); 
while (matcher.find()) { 
    System.out.println(matcher.group()); 
} 
0

只是爲了好玩,使用replaceAll

String output = input.replaceAll("(?s)(\\]\\]|^).*?(\\[\\[|$)", "$1\n$2");