【发布时间】:2015-01-31 08:18:20
【问题描述】:
我的文件包含几行字符串,写成:
[(W)40(indo)25(ws )20(XP)111(, )20(with )20(the )20(fragment )20(enlar)18(ged )20(for )20(clarity )20(on )20(Fig. )] TJ
我只需要括号内的文字。我尝试使用以下代码:
import re
readstream = open ("E:\\New folder\\output5.txt","r").read()
stringExtract = re.findall('\[(.*?)\]', readstream, re.DOTALL)
string = re.compile ('\(.*?\)')
stringExtract2 = string.findall (str(stringExtract))
但输出中不存在某些字符串(或文本),例如,对于上述字符串,输出中未找到单词 (with)。另外字符串的排列方式与文件不同,比如上面的字符串(enlar)和(ged),第二个(ged)出现在(enlar)之前,比如:(ged other strings..... enlar) How我可以解决这些问题吗?
【问题讨论】:
-
在陈述模式时始终使用原始字符串
标签: python regex python-2.7