如何在采用RE2库(不支持环视)的Kusto中提取ServiceInstanceId后的目标子字符串?
提取ServiceInstanceId后的目标内容(Kusto + RE2)
当然可以!虽然RE2不支持环视功能,但我们可以通过捕获组精准实现你的需求——不管是提取到下一个空格前的全部内容,还是只提取示例里的UUID部分,都能轻松搞定。
1. 提取"ServiceInstanceId:"到下一个空格前的所有字符
完全匹配你描述的需求,用[^ ]+匹配除空格外的所有字符,结合捕获组锁定目标内容:
// 模拟输入字符串 print input_str = "Some sentence. Some word Some word ServiceInstanceId:78d61d2f-6df9-4ba4-a192-0713d3cd8a82.1234 not found. , ErrorCode:2 Some sentences. Some sentences.Some sentences." // 使用extract函数提取捕获组内容 | extract @"ServiceInstanceId:([^ ]+)" from input_str with (string) as service_instance_full_id
执行后会得到结果:78d61d2f-6df9-4ba4-a192-0713d3cd8a82.1234
2. 只提取UUID部分(去掉后续的.1234)
如果你的真实需求是获取示例里的纯UUID(78d61d2f-6df9-4ba4-a192-0713d3cd8a82),可以利用UUID的格式特征(十六进制字符+连字符)优化正则:
print input_str = "Some sentence. Some word Some word ServiceInstanceId:78d61d2f-6df9-4ba4-a192-0713d3cd8a82.1234 not found. , ErrorCode:2 Some sentences. Some sentences.Some sentences." | extract @"ServiceInstanceId:([a-f0-9-]+)" from input_str with (string) as service_instance_uuid
这个正则会匹配到第一个非十六进制/连字符的字符前的内容,正好得到你想要的UUID。
为什么这两种方式都可行?
- Kusto的
extract()函数专门用于从字符串中提取正则捕获组的内容,完美适配RE2语法。 - 全程没有使用任何环视语法,完全符合RE2的限制,在Kusto中能稳定运行。
内容的提问来源于stack exchange,提问作者Sahil Raj
相关产品推荐
相关产品推荐

