我正在尝试列出过期客户端证书的一些通知,然后发送包含客户端名称和过期日期的电子邮件。
r = self._s.get(self.url)
if r.status_code == 200:
pass
else :
return None
for i in re.finditer('<a id="[A-Z0-9]+" href="/ui/partycfg/remote/Certificate\?partyId=([0-9]+).*?" title=".*?">(.*?)</a>', r.text):
print i.group(1) + ": " + i.group(2)
我可以使用 i.groupe(2)检索列表 例如,列表如下:
partner1's certificate 'partner certif1' expired on 08/23/17
partner2's certificate 'partner certif2' expired on 02/14/18
如何提取合作伙伴名称和日期。 谢谢
答案 0 :(得分:0)
你可以试试
(.*?)'s.*((?:\d{2}\/){2}\d{2})
示例代码:
import re
regex = r"(.*?)'s.*((?:\d{2}\/){2}\d{2})"
test_str = ("partner1's certificate 'partner certif1' expired on 08/23/17\n"
"partner2's certificate 'partner certif2' expired on 02/14/18")
matches = re.finditer(regex, test_str)
for match in matches:
print(match.group(1));
print(match.group(2));
答案 1 :(得分:0)
你不需要正则表达式,正则表达式对此有点矫枉过正。与字符串切片相比,正则表达式非常慢:
data = """partner1's certificate 'partner certif1' expired on 08/23/17
partner2's certificate 'partner certif2' expired on 02/14/18"""
for line in data.split('\n'):
name = line[:line.find("'")]
date = line[-8:]
print "name:", name, "date:", date
输出:
name: partner1 date: 08/23/17
name: partner2 date: 02/14/18