replace()replace()

将所有正则表达式匹配项替换为其他字符串。Replace all regex matches with another string.

语法Syntax

replace(regex, rewrite, text) replace(regex, rewrite, text)

参数Arguments

  • regex:用于搜索 text 的正则表达式regex: The regular expression to search text. 可在“(括号)”中包含捕获组。It can contain capture groups in '('parentheses')'.
  • rewrite:matchingRegex 执行的任何匹配的替换正则表达式。rewrite: The replacement regex for any match made by matchingRegex. 使用 \0 引用整个匹配项,\1 用于第一个捕获组,\2 等用于后续捕获组。Use \0 to refer to the whole match, \1 for the first capture group, \2 and so on for subsequent capture groups.
  • text:一个字符串。text: A string.

返回Returns

使用 rewrite 计算结果替换 regex 的所有匹配项后的 texttext after replacing all matches of regex with evaluations of rewrite. 匹配项不重叠。Matches do not overlap.

示例Example

此语句:This statement:

range x from 1 to 5 step 1
| extend str=strcat('Number is ', tostring(x))
| extend replaced=replace(@'is (\d+)', @'was: \1', str)

具有以下结果:Has the following results:

xx strstr 替换的内容replaced
11 数值为 1.000000Number is 1.000000 数值曾为:1.000000Number was: 1.000000
22 数值为 2.000000Number is 2.000000 数值曾为:2.000000Number was: 2.000000
33 数值为 3.000000Number is 3.000000 数值曾为:3.000000Number was: 3.000000
44 数值为 4.000000Number is 4.000000 数值曾为:4.000000Number was: 4.000000
55 数值为 5.000000Number is 5.000000 数值曾为:5.000000Number was: 5.000000