使用正则表达式提取第一段文字

2024-10-01 09:33:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我想摘录第一段。但我找到了好运。有人能帮我吗?这是文本。http://dpaste.com/638776/。我的文字是动态的。谢谢

更新:我正在使用eTree模块读取XML文件。在XML中有一个名为<text></text>的标记。<text></text>is here之间的数据。我只想从text tags打印以下数据。有可能吗?谢谢

  '''Zamindar''' ({{te|జమీందార్}}) is a 1965 [[Telugu language|Telugu]] "Thriller" film 
  directed by [[V. Madhusudhan Rao]] and produced by [[Tammareddy Krishna Murthy]] 
  of Ravindra Art Pictures.This is variety role for [[Akkineni Nageswara Rao]]
  who is more popular with soft Romantic roles.He plays the role of a tough CID Officer     very well.The Movie has some Good songs.This movie has a considerable resemblance with the 1963 [[Cary Grant]] English Movie ''[[Charade (1963 film)|Charade]]''.

Tags: ofthe数据textbyiswithxml
2条回答

如果您构建了一个点与换行符匹配的regex,那么您已经(在ruby中测试过了,但是我想它将在python中正常工作)。这与尼尔·伯恩的回答完全相同:

}}\n(.*?)\n\n

请在rubular处查看效果。在

根据新信息修订。。。在

如果能够在标记之间生成文本,则只需为第一段找到适合所有情况的模式,因此基于以下示例:

 #data - stuff between text tags
 firstparagraph = re.search("}}(.*?)\r*\n\r*\n",data,re.DOTALL)
 print firstparagraph.group(1)

相关问题 更多 >