我尝试为 Splunk 搜索制作一个正则表达式,它应该从 URL 中提取 TLD。来源是全景日志。
正则表达式:
^(?:https?:\/\/)?(?<host>[^\/]+)?(?<tld>\.[^.?\/\n]+).*$
测试数据:
https://example.org/
qq.com
https://border.example.com/?bridge=basket&blood=animal
360.cn
http://example.com/?brother=bike
smugmug.com
shop-pro.jp
正则表达式和测试数据在Regex101.com;我使用 randomlists.com 生成测试数据以匿名化源数据。需要捕获组
描述你尝试了什么,
从一组 URL 中匹配 TLD;有些有先前的协议,有些没有。输入记录应以换行符分隔,匹配项不应超过一条记录。
你期望发生什么,
所有 TLD 都匹配并在捕获组中
以及实际结果。
以
/
结尾的行,但没有 don't 的行。
rex
,这一切都可以用eval
和mvexpand
来完成
随处运行的例子:
| makeresults
| eval urls="https://www.example.org/|http://example.com/|ca.gov|http://blade.example.com/bikes/airplane.php|http://alarm.example.com/|smugmug.com|shop-pro.jp|https://example.org/|qq.com|pcworld.com|symantec.com|360.cn|http://example.com/?brother=bike|http://www.example.com/behavior/bead.php|army.mil|https://example.com/boy/bedroom.php|https://example.com/|https://www.example.com/brother?activity=believe|https://www.example.net/achiever/bottle.html|http://believe.example.com/bit?bait=base&bone=ball|aboutads.info|http://www.example.com/|http://www.example.edu/afternoon|livejournal.com|http://border.example.com/box/afterthought|oaic.gov.au|https://www.example.edu/base.php|house.gov|smh.com.au|http://www.example.edu/|https://www.example.org/|lycos.com|https://border.example.com/?bridge=basket&blood=animal|hibu.com|http://example.com/"
| eval urls=split(urls,"|")
| mvexpand urls
| eval busted=split(urls,":")
| eval busted=mvindex(trim(split(trim(replace(mvfilter(match(busted,"\.")),"\/"," "))," ")),0)
我将最后几个步骤合并为一行,但这就是它正在做的:
|
”)字符打破 URL 列表mvexpand
多值字段split
:
字符上的每个单独的 URL(如果它不存在,则没有任何内容 split
match
中选择以下split
edmvfilter
的第0个(第一个)元素:
.
”)的东西/
”)替换为空格(“
”)和
”)您想要的 fqdn 现在在
busted
提取 TLD 现在很简单。附加以下内容:
| rex field=busted "(?<tld>[0-9a-zA-Z][0-9a-zA-Z_\-]+?\.[0-9a-zA-Z]+)$"
或者,为了只保留一个
eval
,完全跳过rex
,这样做:
| eval tld=mvindex(split(busted,"."),-2) +"."+ mvindex(split(busted,"."),-1)
@Casimir et Hippolyte 发布,此解决方案解决了我的问题:
就足够了。 (用一行你可以删除所有/^[^.\n]*[^\/\n]*\.\K[^\/\n]+/gm
)\n
编辑:我想补充一点,我没有将其标记为“Community Wiki”的原因不是因为我不想,而是因为我没有足够的声誉来选择这样做( https://meta.stackexchange.com/questions/11740/what-are-community-wiki-posts 请参阅“帖子如何成为社区 Wiki 帖子?”)