![](/img/trans.png)
[英]Python - re.search and/or re.finditer not matching simple pattern
[英]python re.search() is not matching the pattern,
python 2.7.3:我确定我缺少一些简单的东西,但无法弄清楚我有一个文本文件,并且正在逐行读取文件:
dnsfile = open(self.logfile, "r")
for i, line in enumerate(dnsfile):
我为某种模式匹配的每一行:
match = re.search(r'(.*mail.*?) (.*) NS (.*)', line)
if match:
print "Matched"
else:
print "No match"
我在文件中的所有文件都显示“ No match”,我正在查找以下行:
outlook.mail.com. 269 NS ns1.msft.net.
mailxyz.com. 123695 NS adns1.apple.com
我正在http://pythex.org/上尝试相同的模式和行及其匹配项,但是没有使用此代码。
您的DNS文件中有制表符,而不是空格。 尝试这个:
match = re.search(r'(.*mail.*?)\s+(.*)\s+NS\s+(.*)', line)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.