Python 要使用正则表达式分析段落吗

Python 要使用正则表达式分析段落吗,python,regex,Python,Regex,我有下面这样的段落。我只想忽略文本[[戏剧电影|电影]]。输出应该是电影。 另一个例子[[Scotland | Scottish]]输出应该是苏格兰语 如果没有管道[[Eva Birthistle]],则输出应仅为Eva Birthistle 输出我想要的 可能吗 这是。我试过这个方法。但那一个不起作用。我将非常感激。非常感谢你 另一个解决方案不起作用,因为您的问题不清楚。这是这个问题的答案 >>> x="Ae Fond Kiss... (also known as ''Jus

我有下面这样的段落。我只想忽略文本[[戏剧电影|电影]]。输出应该是电影。 另一个例子[[Scotland | Scottish]]输出应该是苏格兰语 如果没有管道[[Eva Birthistle]],则输出应仅为Eva Birthistle

输出我想要的

可能吗


这是。我试过这个方法。但那一个不起作用。我将非常感激。非常感谢你

另一个解决方案不起作用,因为您的问题不清楚。这是这个问题的答案

>>> 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.
>>> 

另一个问题就在眼前:

>>> 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...'

如果正则表达式不起作用,为什么要接受这个问题的答案?还有,它不起作用了吗?@Wilduck这是我的另一个问题。现在一切正常。在您的示例输出中,[[戏剧性电影|电影]]变成电影,[[Scotland | Scottish]]变成与您描述不符的苏格兰,[[Ae FondKiss | Ae FondKiss]]变成Ae FondKiss。你如何决定使用每组中的第一个或第二个元素?它缺少一些单词。请试一试。《恋爱中的英雄希特勒》是一部2011年由[[Sukhjant Dadda]]导演的旁遮普[[浪漫]]和[[动作片|动作]]电影,由[[Babbu Maan]]讲述故事、编剧和对话。《英雄希特勒》由B S Maan Khantmanpur和[[Darshan Singh Grewal]]以及[[Babbu Maan]]、[[Mouni Roy]]和[[Bhagwant Mann]]主演。《恋爱中的英雄希特勒》于11月18日在几乎所有旁遮普剧院上映。该片的评论褒贬不一,但票房开局不错。谢谢你的提醒。我已经更正了正则表达式
>>> 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.
>>> 
>>> 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...'