使用Python3在域名regex中进行高级分组

使用Python3在域名regex中进行高级分组,regex,python-3.x,regex-group,domain-name,Regex,Python 3.x,Regex Group,Domain Name,我有一个用python3编写的程序,它应该每天解析几个域名,并推断数据。 解析后的数据应作为搜索函数、聚合(统计和图表)的输入,并为使用该程序的分析员节省一些时间 正如你所知:我真的没有时间学习机器学习(这似乎是一个很好的解决方案),所以我选择从我已经使用的正则表达式开始。 我已经在StackOverflow内外搜索了regex文档,并在regex101上使用了调试器,但仍然没有找到一种方法来完成我需要的工作。 编辑(2019年6月24日):我之所以提到机器学习,是因为我需要一个复杂的解析器,即

我有一个用python3编写的程序,它应该每天解析几个域名,并推断数据。
解析后的数据应作为搜索函数、聚合(统计和图表)的输入,并为使用该程序的分析员节省一些时间

正如你所知:我真的没有时间学习机器学习(这似乎是一个很好的解决方案),所以我选择从我已经使用的正则表达式开始。
我已经在StackOverflow内外搜索了regex文档,并在regex101上使用了调试器,但仍然没有找到一种方法来完成我需要的工作。
编辑(2019年6月24日):我之所以提到机器学习,是因为我需要一个复杂的解析器,即尽可能地自动化事情。它将有助于做出诸如黑名单、白名单等自动选择

解析器应该考虑一些事情:

  • 最多126个子域加上TLD
  • 每个子域的长度不得超过64个字符
  • 每个子域只能包含字母数字字符和
    -
    字符
  • 每个子域不得以
    -
    字符开头或结尾
  • TLD的长度不得超过64个字符
  • TLD不能仅包含数字
但我想再深入一点:

  • 第一个字符串可以(可选)包含“用法类型”,如
    cpanel.
    mail.
    webdisk.
    自动发现。
    等等。。。(或者可能是一个交响乐
    www.
  • TLD可以(可选)包含类似
    .co
    .gov
    .edu
    等粒子(例如
    .co.uk
  • TLD的最后一部分目前没有根据任何ccTLD/GTL列表进行检查,我认为将来也不会进行检查
我认为解决这个问题有用的是一个用于可选使用类型的正则表达式组,一个用于每个子域,一个用于TLD(可选粒子必须位于TLD组内)
考虑到这些规则,我想出了一个解决方案:

^(?P<USAGE>autodiscover|correo|cpanel|ftp|mail|new|server|webdisk|webhost|webmail[\d]?|wiki|www[\d]?\.)?([a-z\d][a-z\d\-]{0,62}[a-z\d])?((\.[a-z\d][a-z\d\-]{0,62}[a-z\d]){0,124}?(?P<TLD>(\.co|\.com|\.edu|\.net|\.org|\.gov)?\.(?!\d+)[a-z\d]{1,64})$
我希望找到的组

without.further.ado.lets.travel.the.forest.com  
www.without.further.ado.lets.travel.the.forest.gov.it  
  • FullMatch
    without.further.ado.lets.travel.the.forest.com

    组2

    第3组
    进一步

    组4
    ado

    第5组
    允许

    第6组
    旅行

    第7组
    the

    组8
    forest

    groupTLD
    .com
  • FullMatch
    www.without.further.ado.lets.travel.the.forest.gov.it

    组用法
    www.

    组2

    第3组
    进一步

    组4
    ado

    第5组
    允许

    第6组
    旅行

    第7组
    the

    组8
    forest

    groupTLD
    .gov.it
我找到的群组

without.further.ado.lets.travel.the.forest.com  
www.without.further.ado.lets.travel.the.forest.gov.it  
  • FullMatch
    without.further.ado.lets.travel.the.forest.com

    组2

    group3
    .furth.ado.lets.travel.the.forest

    组4
    .forest

    groupTLD
    .com
  • FullMatch
    www.without.further.ado.lets.travel.the.forest.gov.it

    组用法
    www.

    组2

    group3
    .furth.ado.lets.travel.the.forest

    组4
    .forest

    groupTLD
    .gov.it

    第6组
    .gov

正如您从示例中看到的,一对粒子被发现了两次,这不是我所寻求的行为。任何编辑公式的尝试都会导致未过期的输出。

有什么办法可以找到预期的结果吗?

我不知道是否有可能完全按照您的要求获得输出。我认为单一模式无法捕捉不同组(第2组、第3组……)的结果

我找到了一种方法,使用该模块可以获得您期望的结果

在这里,为了避免在组中使用
,我将它添加到了非捕获组中,如下所示

(?:([a-z\d][a-z\d\-]{0,62}[a-z\d])\.)

希望有帮助。

这是一项简单、定义明确的任务。这里没有模糊性,没有复杂性,没有猜测,只有一系列简单的测试来找出清单上的所有内容。我不知道“机器学习”有多合适,有多有用。甚至正则表达式也完全没有必要

我还没有实现您想要验证的所有功能,但是要填充缺少的部分并不困难

import string

double_tld = ['gov', 'edu', 'co', 'add_others_you_need']

# we'll use this instead of regex to check subdomain validity
valid_sd_characters = string.ascii_letters + string.digits + '-'
valid_trans = str.maketrans('', '', valid_sd_characters)

def is_invalid_sd(sd):
    return sd.translate(valid_trans) != ''

def check_hostname(hostname):
    subdomains = hostname.split('.')

    # each subdomain can contain only alphanumeric characters and
    # the - character
    invalid_parts = list(filter(is_invalid_sd, subdomains))
    # TODO react if there are any invalid parts

    # "the TLD can (optionally) contain a particle like
    # .co, .gov, .edu and so on (.co.uk for example)"
    if subdomains[-2] in double_tld:
        subdomains[-2] += '.' + subdomains[-1]
        subdomains = subdomains[:-1]

    # "a maximum number of 126 subdomains plus the TLD"
    # TODO check list length of subdomains

    # "each subdomain must not begin or end with the - character"
    # "the TLD must not be longer than 64 characters"
    # "the TLD must not contain only digits"
    # TODO write loop, check first and last characters, length, isnumeric

    # TODO return something

正则表达式在这里似乎不合适。
split(“.”
基本上不做这项工作吗?您可以创建白名单域和扩展集,并检查成员资格。检查
result[-1]
是否为域集中的成员身份,如果不匹配,请使用
result[-2:]
作为您的组TLD。并且可能在任何操作之前注册
以避免其他检查?或者我应该马上忘记regex吗?我不会完全排除regex。这个问题是非常合理的,但我建议一些改进:1)删除后台(谢谢,但它会产生噪音,混淆需求,这对我来说不容易理解)。2) 把你要解决的问题清楚地说出来,与你的工作无关。3) 说明您的要求,描述所有边缘情况。4) 张贴你的例子,尝试并展示它是如何不起作用的(你在这里看起来不错)。这可能有助于使问题更容易理解。我重写了问题。。。像这样清楚吗?为什么机器学习在所有事情中是一个好的解决方案?这是关于拆分字符串的,你甚至不需要正则表达式。对不起,我没有解释我自己:机器学习将有助于自动选择,如黑名单、白名单等。基于当时的发现啊,我误解了。虽然,当你已经知道你允许什么和禁止什么的时候,你为什么不把它列入黑名单呢
import string

double_tld = ['gov', 'edu', 'co', 'add_others_you_need']

# we'll use this instead of regex to check subdomain validity
valid_sd_characters = string.ascii_letters + string.digits + '-'
valid_trans = str.maketrans('', '', valid_sd_characters)

def is_invalid_sd(sd):
    return sd.translate(valid_trans) != ''

def check_hostname(hostname):
    subdomains = hostname.split('.')

    # each subdomain can contain only alphanumeric characters and
    # the - character
    invalid_parts = list(filter(is_invalid_sd, subdomains))
    # TODO react if there are any invalid parts

    # "the TLD can (optionally) contain a particle like
    # .co, .gov, .edu and so on (.co.uk for example)"
    if subdomains[-2] in double_tld:
        subdomains[-2] += '.' + subdomains[-1]
        subdomains = subdomains[:-1]

    # "a maximum number of 126 subdomains plus the TLD"
    # TODO check list length of subdomains

    # "each subdomain must not begin or end with the - character"
    # "the TLD must not be longer than 64 characters"
    # "the TLD must not contain only digits"
    # TODO write loop, check first and last characters, length, isnumeric

    # TODO return something