2011-12-06 37 views
0

我有像下面這樣的段落。我只是想忽略文字[[drama film|film]]。輸出應該是film。 另一個例子[[Scotland|Scottish]]輸出應該是Scottish 如果沒有Pipe [伊娃·伯錫斯爾]然後輸出應該只有Eva Birthistle想使用正則表達式分析段落

Ae Fond Kiss... (also known as ''Just a Kiss'') is a 2004 romantic [[drama film|film]] 
directed by [[Ken Loach]], and starring [[Atta Yaqub]] and [[Eva Birthistle]]. The 
title is taken from a [[Scotland|Scottish]] song by [[Robert Burns]], the complete 
line being ''[[Ae Fond Kiss|Ae fond kiss]], and then we sever...' 

Output I want

Ae Fond Kiss... (also known as ''Just a Kiss'') is a 2004 romantic film 
directed by Ken Loach, and starring Atta Yaqub and Eva Birthistle. The 
title is taken from a Scotland song by Robert Burns, the complete 
line being 'Ae Fond Kiss, and then we sever...' 

這可能嗎?

這裏是duplicate question。我在這方面嘗試過這個解決方案。但那個不起作用。我將非常感激。非常感謝。

+0

你爲什麼要接受一個問題的答案,如果正則表達式是不工作?還有,它不起作用呢? – Wilduck

+0

@Wilduck這是我的另一個問題。現在工作正常。用這個解決方案。 – Kulbir

+0

在您的示例輸出中,'[[戲劇電影]]'變成'電影','[[蘇格蘭|蘇格蘭]]'變成'蘇格蘭'(與您的描述不符),'[[Ae Fond Kiss | Ae親吻]]'變成Ae親吻。你如何決定是否使用每個組的第一個或第二個元素? –

回答

0

其他解決方案沒有工作,因爲你的問題不清楚。這是解決這個問題

>>> x="Ae Fond Kiss... (also known as ''Just a Kiss'') is a 2004 romantic [[drama film|film]]\ndirected by [[Ken Loach]], and starring [[Atta Yaqub]] and [[Eva Birthistle]]. The \ntitle is taken from a [[Scotland|Scottish]] song by [[Robert Burns]], the complete\nline being ''[[Ae Fond Kiss|Ae fond kiss]], and then we sever...'" 
>>> print re.sub("([^\[\]]*\|)|(\[\[)|(\]\])","",x) 
Ae Fond Kiss... (also known as ''Just a Kiss'') is a 2004 romantic film 
directed by Ken Loach, and starring Atta Yaqub and Eva Birthistle. The 
title is taken from a Scottish song by Robert Burns, the complete 
line being ''Ae fond kiss, and then we sever...' 
>>> x="Hero Hitler In Love is a 2011 Punjabi [[romantic]] and [[action film|action]] film directed by [[Sukhjant Dadda]] with story, screenplay & dialogues by [[Babbu Maan]]. Hero Hitler is produced by B S Maan (Khantmanpur) and [[Darshan Singh Grewal]], and stars [[Babbu Maan]], [[Mouni Roy]] and [[Bhagwant Mann]]. ''Hero Hitler In Love'' was released on 18 November in mostly all Punjab theaters. It released to mixed reviews but did have a good opening at the box office." 
>>> print re.sub("([^\[\]]*\|)|(\[\[)|(\]\])","",x) 
Hero Hitler In Love is a 2011 Punjabi romantic and action film directed by Sukhjant Dadda with story, screenplay & dialogues by Babbu Maan. Hero Hitler is produced by B S Maan (Khantmanpur) and Darshan Singh Grewal, and stars Babbu Maan, Mouni Roy and Bhagwant Mann. ''Hero Hitler In Love'' was released on 18 November in mostly all Punjab theaters. It released to mixed reviews but did have a good opening at the box office. 
>>> 
+0

它缺少一些詞。請嘗試。 「英雄希特勒愛情」是[[Sukhjant Dadda]導演的2011年旁遮普語[[浪漫]]和[[動作電影|動作]]電影,由[[Babbu Maan]]講故事,劇本和對話。英雄希特勒由B S Maan(Khantmanpur)和[Darshan Singh Grewal]以及明星[Babbu Maan],[[Mouni Roy]]和[Bhagwant Mann]製作。 「英雄希特勒戀愛」於11月18日在大多數旁遮普電影院上映。它發佈了混合評論,但在票房確實有一個很好的開幕。「# – Kulbir

+0

Offcourse它做到了:-)。感謝提及。我糾正了正則表達式 – Abhijit

0

,其他問題不是遠:

>>> pattern = re.compile(r'\[\[(?:.*?\|)?(.*?)\]\]') 
>>> input = """Ae Fond Kiss... (also known as ''Just a Kiss'') is a 2004 romantic [[drama film|film]] 
directed by [[Ken Loach]], and starring [[Atta Yaqub]] and [[Eva Birthistle]]. The 
title is taken from a [[Scotland|Scottish]] song by [[Robert Burns]], the complete 
line being ''[[Ae Fond Kiss|Ae fond kiss]], and then we sever...'""" 
>>> print(pattern.sub("\\1", input)) 
Ae Fond Kiss... (also known as ''Just a Kiss'') is a 2004 romantic film 
directed by Ken Loach, and starring Atta Yaqub and Eva Birthistle. The 
title is taken from a Scottish song by Robert Burns, the complete 
line being ''Ae fond kiss, and then we sever...'