我使用以下代码来查找文本文件中的所有域(尽我所能)。问题是它没有找到任何东西。我已经在 regex101 上测试了正则表达式,它匹配得很好。谁能指出问题所在吗? Tld.txt 包含完整的小写 TLD 列表,因为我想搜索所有这些列表。
编辑:
Tld.txt 看起来像这样 -
com in
domains.txt looks like this-
mplay.google.co.in play.google.com
Code
import re
with open("tld.txt", "r") as f:
tld = f.read().splitlines()
with open("domains.txt","r") as f:
domains = f.read().splitlines()
for x in tld:
regex = "^(.*?)"+str(x)
for y in domains:
domains_found = re.findall(regex, y)
print domains_found
最佳答案
您正在打印最后的结果,因为您没有将结果添加到domains_found
,而是替换其内容。你刚刚尝试过这个吗?
import re
with open("tld.txt", "r") as f:
tld = f.read().splitlines()
with open("domains.txt","r") as f:
domains = f.read().splitlines()
for x in tld:
regex = "^(.*?)"+str(x)
for y in domains:
domains_found = re.findall(regex, y)
print domains_found
或者更好
domains_found.extend(re.findall(regex, y))
关于python - 正则表达式用于搜索文本文件中的域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43898894/