正则表达式以“/”字符代替换行符

问题描述 投票:0回答:2

我尝试为 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 的行。

regex pcre splunk
2个回答
0
投票

而不是使用

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
    ed
    mvfilter
    的第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)

0
投票

@Casimir et Hippolyte 发布,此解决方案解决了我的问题:

是否需要匹配所有线路?如果您只想要 TLD

/^[^.\n]*[^\/\n]*\.\K[^\/\n]+/gm
就足够了。 (用一行你可以删除所有
\n

编辑:我想补充一点,我没有将其标记为“Community Wiki”的原因不是因为我不想,而是因为我没有足够的声誉来选择这样做( https://meta.stackexchange.com/questions/11740/what-are-community-wiki-posts 请参阅“帖子如何成为社区 Wiki 帖子?”)

© www.soinside.com 2019 - 2024. All rights reserved.