从字符串中解析多个 FQDN
Parsing several FQDNs from string
给定一个主域,我试图从字符串中提取它及其子域。
例如对于主域 example.co
我想:
- 仅提取主域和子域 -
example.co
、www.example.co
、uat.smile.example.co
- 不拾取向右延伸的名称 - 否
www.example.com
、www.example.co.nz
- 忽略 FQDN 中不合法的任何 space 或标点符号作为分隔符
目前我从以下渠道收到不需要的物品:
example.com
example.co.nz
test-me.www.example.co
还包括结尾的 space.
>>> domain = 'example\.co'
>>> line = 'example.com example.co.nz www.example.co. test-me.www.example.co bad.example-co.co'
>>> re.findall("[^\s\',]*{}[\s\'\,]*".format(domain), line)
['example.co', 'example.co', 'www.example.co', 'test-me.www.example.co ']
我应该使用正则表达式吗?如果是这样,将不胜感激有关解决此问题的指导。
否则还有更好的工具吗?
编辑 - 已验证 Marc Lambrichs 的回答,但在以下情况下失败:
import re
pattern = r"((?:[a-zA-Z][\w-]+\.)+{}(?!\w))"
domain = 'google.com'
line = 'google.com mail is handled by 20 alt1.aspmx.l.google.com.'
results = re.findall(pattern.format(re.escape(domain)), line)
print(results)
[]
另外,我想传递像 'google.com' 这样的字符串而不是 'google.com' 并用 re
转义但是 re.escape(domain)
代码 returns 空列表.
您可以为此使用 regex 而无需任何拆分。
$ cat test.py
import re
tests = { 'example.co': 'example.com example.co.nz www.example.co. test-me.www.example.co bad.example-co.co',
'google.com': 'google.com mail is handled by 20 alt1.aspmx.l.google.com.'}
pattern = r"((?:[a-zA-Z][-\w]*\.)*{}(?!\w))"
for domain,line in tests.iteritems():
domain = domain.replace(".", "\.")
results = re.findall(pattern.format(domain), line)
print results
给出结果:
$ python test.py
['google.com', 'alt1.aspmx.l.google.com']
['example.co', 'www.example.co', 'test-me.www.example.co']
正则表达式的解释
( # group 1 start
(?: # non-capture group
[a-zA-Z] # rfc 1034. start subdomain with a letter
[\w-]*\. # 0 or more word chars or '-', followed by '.'
)* # repeat this non-capture group 0 or more times
example.co # match the domain
(?!\w) # negative lookahead: no following word char allowed.
) # group 1 end
给定一个主域,我试图从字符串中提取它及其子域。
例如对于主域 example.co
我想:
- 仅提取主域和子域 -
example.co
、www.example.co
、uat.smile.example.co
- 不拾取向右延伸的名称 - 否
www.example.com
、www.example.co.nz
- 忽略 FQDN 中不合法的任何 space 或标点符号作为分隔符
目前我从以下渠道收到不需要的物品:
example.com
example.co.nz
test-me.www.example.co
还包括结尾的 space.
>>> domain = 'example\.co'
>>> line = 'example.com example.co.nz www.example.co. test-me.www.example.co bad.example-co.co'
>>> re.findall("[^\s\',]*{}[\s\'\,]*".format(domain), line)
['example.co', 'example.co', 'www.example.co', 'test-me.www.example.co ']
我应该使用正则表达式吗?如果是这样,将不胜感激有关解决此问题的指导。
否则还有更好的工具吗?
编辑 - 已验证 Marc Lambrichs 的回答,但在以下情况下失败:
import re
pattern = r"((?:[a-zA-Z][\w-]+\.)+{}(?!\w))"
domain = 'google.com'
line = 'google.com mail is handled by 20 alt1.aspmx.l.google.com.'
results = re.findall(pattern.format(re.escape(domain)), line)
print(results)
[]
另外,我想传递像 'google.com' 这样的字符串而不是 'google.com' 并用 re
转义但是 re.escape(domain)
代码 returns 空列表.
您可以为此使用 regex 而无需任何拆分。
$ cat test.py
import re
tests = { 'example.co': 'example.com example.co.nz www.example.co. test-me.www.example.co bad.example-co.co',
'google.com': 'google.com mail is handled by 20 alt1.aspmx.l.google.com.'}
pattern = r"((?:[a-zA-Z][-\w]*\.)*{}(?!\w))"
for domain,line in tests.iteritems():
domain = domain.replace(".", "\.")
results = re.findall(pattern.format(domain), line)
print results
给出结果:
$ python test.py
['google.com', 'alt1.aspmx.l.google.com']
['example.co', 'www.example.co', 'test-me.www.example.co']
正则表达式的解释
( # group 1 start
(?: # non-capture group
[a-zA-Z] # rfc 1034. start subdomain with a letter
[\w-]*\. # 0 or more word chars or '-', followed by '.'
)* # repeat this non-capture group 0 or more times
example.co # match the domain
(?!\w) # negative lookahead: no following word char allowed.
) # group 1 end