python - 正则表达式用于搜索文本文件中的域

标签 python regex

我使用以下代码来查找文本文件中的所有域(尽我所能)。问题是它没有找到任何东西。我已经在 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/

相关文章:

python - 当我运行 Flask 时,它显示错误 : ModuleNotFoundError: No module named 'werkzeug.contrib' . 谁能帮我解决这个问题?

python - PyPI 上轮子的平台约束有任何限制吗?

python - 查找列表项的索引

Javascript正则表达式选择没有特殊字符的单词

python - 列表的列表..列表的列表?应用正则表达式和 nltk

python - 从 CSV 文件中的邻接矩阵绘制 NetworkX 图

javascript - 不匹配特定字符串后的字符串的正则表达式

regex - 如何仅在 Scala 中小写捕获组

regex - 在 VBA 中对数学(中缀)表达式进行标记

python - 我不明白 Python 的主要 block 。那个东西是什么?