简体   繁体   English

python re.search()与模式不匹配,

[英]python re.search() is not matching the pattern,

python 2.7.3: I'm sure I'm missing something simple but not able to figure it out I have a text file and I am reading file line by line: python 2.7.3:我确定我缺少一些简单的东西,但无法弄清楚我有一个文本文件,并且正在逐行读取文件:

dnsfile = open(self.logfile, "r")
        for i, line in enumerate(dnsfile):

each line i am matching for some pattern: 我为某种模式匹配的每一行:

match = re.search(r'(.*mail.*?) (.*) NS (.*)', line)
if match:
    print  "Matched"
else:
    print "No match"

I am getting "No match" for all in file I have following lines which i am looking : 我在文件中的所有文件都显示“ No match”,我正在查找以下行:

outlook.mail.com.               269     NS      ns1.msft.net.
mailxyz.com.          123695  NS      adns1.apple.com

I am trying same pattern and line on http://pythex.org/ and its matching but not with this code. 我正在http://pythex.org/上尝试相同的模式和行及其匹配项,但是没有使用此代码。

You have tabs, not spaces, in your DNS file. 您的DNS文件中有制表符,而不是空格。 Try this: 尝试这个:

match = re.search(r'(.*mail.*?)\s+(.*)\s+NS\s+(.*)', line)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM