超大fi上的慢速/挂起python正则表达式

2024-05-26 17:43:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在写一些python,其中包括在内存转储中搜索潜在的url。这个程序对我来说似乎运行得很好,但是一个测试过它的用户说,它在这个代码药水上挂了至少两天:

#carve urls
print "\nCarving potential URLs from correlated strings."
pattern = re.compile(ur'(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?\xab\xbb\u201c\u201d\u2018\u2019]))')
with open(outputPath+"\\strings\\correlatedstrings.txt", "r") as fstring:
    with createFile(outputPath+"\\strings", "urlSearch") as f:
        for stringline in fstring:
            if pattern.search(stringline):
                f.write(stringline)

正则表达式是我在网上找到的(可能就在这里)。它似乎对我非常有效。这里的一个潜在问题是,这个用户正在搜索的内存转储高达32GB。在超大文件上使用regex或我的代码有问题吗?任何想法都会很有帮助:)。你知道吗


Tags: 内存代码用户程序urlaswithurls

热门问题