Python新手:
我试图在文本文件上进行正则表达式搜索 例如,如果我有这样的文本文件:
Datetime somewords
Datetime morewords
Datetime somemorewords
Datetime Merging 8 files with Email Address = 'someone@something.com'"
Datetime somewords
Datetime Merging 2 files with Email Address = 'someone@something.com'"
我试图提取这句话: "将文件与电子邮件地址合并=' someone@something.com'"
我正在尝试使用的正则表达式如下:但是这将搜索具有合并和数字的任何内容。
我可以将数字和电子邮件地址组合在一起以便解析器返回以下输出的最佳方式是什么?
期望的输出:
Merging 8 files with Email Address = 'someone@something.com'"
Merging 2 files with Email Address = 'someone@something.com'"
我试用的正则表达式:
re.match('r'Merging\s*\d+\.{0,1}\d*\Files with Email Address\s*=\s*',line);
答案 0 :(得分:1)
使用re.findall
代替re.match
。
Merging\s+\d+[^=]*=\s*'\w+@\w+\.\w+'\"
>>> import re
>>> s = """Datetime somewords
Datetime morewords
Datetime somemorewords
Datetime Merging 8 files with Email Address = 'someone@something.com'\"
Datetime somewords
Datetime Merging 2 files with Email Address = 'someone@something.com'\""""
>>> m = re.findall(r'Merging\s+\d+[^=]*=\s*\'\w+@\w+\.\w+\'\"', s, re.M)
>>> for i in m:
print i
Merging 8 files with Email Address = 'someone@something.com'"
Merging 2 files with Email Address = 'someone@something.com'"