【发布时间】:2019-05-30 22:12:42
【问题描述】:
我正在尝试在文件中查找以 [ 开头并以 ] 结尾的行。我正在使用正则表达式,但无法得到结果。
我尝试过使用各种选项的正则表达式,例如 \s、\S、\w 和 \W。
import re
infile=open("C:\\Users\\Downloads\\Files\\processed.csv","r")
myregex = re.compile(r'(^\[)(\]$)')
list=[]
for groups in myregex.findall(infile.read()):
item=''.join(groups)
cleanitem=item.replace('\n','')
list.append(cleanitem)
print (list)
infile.close()
它应该打印所有以 [ 开头并以 ] 结尾的行。
我该如何解决这个问题?
【问题讨论】:
标签: python regex regex-lookarounds regex-group regex-greedy