You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在KQL中使用正则表达式列表调用replace_regex函数?

在KQL中高效批量替换URL中的ID以实现分组统计

问题背景

我在Azure环境中使用Kusto查询语言(KQL)对Dependencies表的活动数据进行汇总统计,但Name字段是多种格式的URL,其中包含客户ID,导致无法正常分组统计。我已准备好一组用于过滤ID的正则表达式,希望找到比嵌套调用replace_regex更高效、优雅的实现方式。

示例表

NameSuccess
api/path?id=0150403650True
api/path?id=0150403651False
another/path/0150403611/validateTrue
another/path/0150403612/validateFalse
more/paths/4863FDA918BB42959C88E06D72E04238/etcTrue
more/paths/4863FDA918BB42959C88E06D72E04238/etcFalse

期望结果

NameSuccessCount
api/path?id=idTrue1234
api/path?id=idFalse1234
another/path/id/validateTrue1234
another/path/id/validateFalse1234
more/paths/guid/etcTrue1234
more/paths/guid/etcFalse1234

当前可运行但不够优雅的写法

dependencies
| extend call = replace_regex(replace_regex(name, @"[\/]?[a-zA-Z0-9]{32}", "guid"), @"[\/]+[0-9]{10}", "id")
| summarize c = count(call) by call, success

设想的写法(不可行)

let regs = dynamic([@"[\/]?[a-zA-Z0-9]{32}", @"[\/]+[0-9]{10}", @"[=]{1}[0-9]{10}"]);
dependencies
//| where name matches regex regs
| extend call = replace_regex(name, regs, "")
| summarize c = count(call) by call, success

解决方案

KQL的replace_regex不支持直接传入正则表达式数组批量替换,但可以通过以下两种方式实现更优雅的批量替换:

方式1:使用reduce迭代替换规则(易维护)

定义包含正则模式和对应替换值的规则数组,通过reduce遍历规则依次替换,避免嵌套写法:

let replace_rules = dynamic([
    {pattern: @"[\/]?[a-zA-Z0-9]{32}", replacement: "guid"},
    {pattern: @"\/[0-9]{10}", replacement: "/id"},
    {pattern: @"=[0-9]{10}", replacement: "=id"}
]);
dependencies
| extend call = name
| reduce over replace_rules with (call = replace_regex(call, replace_rules.pattern, replace_rules.replacement))
| summarize Count = count() by call, Success

方式2:合并正则分支(性能更优)

将所有正则规则合并为一个模式,通过match_group判断匹配分支并返回对应替换值,适合规则较少的场景:

dependencies
| extend call = replace_regex(name, @"([\/]?[a-zA-Z0-9]{32})|(\/[0-9]{10})|(=[0-9]{10})", 
    iif(match_group(1) != "", "guid", iif(match_group(2) != "", "/id", "=id")))
| summarize Count = count() by call, Success

内容的提问来源于stack exchange,提问作者404usernotfound

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 04:02:20