![](/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.