extract_all()extract_all()

从文本字符串中获取正则表达式的所有匹配项。Get all matches for a regular expression from a text string. 还可以检索匹配组的子集。Optionally, retrieve a subset of matching groups.

print extract_all(@"(\d+)", "a set of numbers: 123, 567 and 789") // results with the dynamic array ["123", "567", "789"]

语法Syntax

extract_all(regex, [captureGroups,] text)extract_all(regex, [captureGroups,] text)

参数Arguments

参数Argument 描述Description 必需或可选Required or Optional
regexregex 一个正则表达式A regular expression. 该表达式必须至少有一个捕获组,并且其捕获组数量必须小于或等于 16 个The expression must have at least one capturing group, and less than or equal to 16 capturing groups 必须Required
captureGroupscaptureGroups 一个动态数组常量,指示要提取的捕获组。A dynamic array constant that indicates the capture group to extract. 有效值为 1 到正则表达式中的捕获组数量。Valid values are from 1 to the number of capturing groups in the regular expression. 还允许命名的捕获组(请参阅示例Named capture groups are allowed as well (See Examples) 可选Optional
texttext 要搜索的 stringA string to search 必须Required

返回Returns

  • 如果 regex 在 text 中找到匹配项,则:返回一个动态数组,其中包括针对所指示的捕获组 captureGroups 或 regex 中的所有捕获组的所有匹配项。If regex finds a match in text: Returns dynamic array including all matches against the indicated capture groups captureGroups, or all of capturing groups in the regex.
  • 如果 captureGroups 的数目为 1,则:返回的数组有一维的匹配值。If number of captureGroups is 1: The returned array has a single dimension of matched values.
  • 如果 captureGroups 的数目大于 1,则:返回的数组是一个二维集合,其中包含针对 captureGroups 选择或 regex 中出现的所有捕获组(如果省略 captureGroups)的多值匹配项。If number of captureGroups is more than 1: The returned array is a two-dimensional collection of multi-value matches per captureGroups selection, or all capture groups present in the regex if captureGroups is omitted.
  • 如果没有匹配项,则返回:nullIf there's no match: null.

示例Examples

提取单个捕获组Extract a single capture group

返回 GUID 的十六进制字节表示形式(两个十六进制数字)。Returns hex-byte representation (two hex-digits) of the GUID.

print Id="82b8be2d-dfa7-4bd1-8f63-24ad26d31449"
| extend guid_bytes = extract_all(@"([\da-f]{2})", Id) 
IDID guid_bytesguid_bytes
82b8be2d-dfa7-4bd1-8f63-24ad26d3144982b8be2d-dfa7-4bd1-8f63-24ad26d31449 ["82","b8","be","2d","df","a7","4b","d1","8f","63","24","ad","26","d3","14","49"]["82","b8","be","2d","df","a7","4b","d1","8f","63","24","ad","26","d3","14","49"]

提取多个捕获组Extract several capture groups

使用带有三个捕获组的正则表达式,将每个 GUID 部分拆分为第一个字母、最后一个字母以及中间部分。Uses a regular expression with three capturing groups to split each GUID part into first letter, last letter, and whatever is in the middle.

print Id="82b8be2d-dfa7-4bd1-8f63-24ad26d31449"
| extend guid_bytes = extract_all(@"(\w)(\w+)(\w)", Id)
IDID guid_bytesguid_bytes
82b8be2d-dfa7-4bd1-8f63-24ad26d3144982b8be2d-dfa7-4bd1-8f63-24ad26d31449 [["8","2b8be2","d"],["d","fa","7"],["4","bd","1"],["8","f6","3"],["2","4ad26d3144","9"]][["8","2b8be2","d"],["d","fa","7"],["4","bd","1"],["8","f6","3"],["2","4ad26d3144","9"]]

提取捕获组的子集Extract a subset of capture groups

说明如何选择捕获组的子集。Shows how to select a subset of capturing groups. 正则表达式匹配第一个字母、最后一个字母以及所有剩余部分。The regular expression matches the first letter, last letter, and all the rest. captureGroups 参数仅用于选择第一个和最后一个部分。The captureGroups parameter is used to select only the first and the last parts.

print Id="82b8be2d-dfa7-4bd1-8f63-24ad26d31449"
| extend guid_bytes = extract_all(@"(\w)(\w+)(\w)", dynamic([1,3]), Id) 
IDID guid_bytesguid_bytes
82b8be2d-dfa7-4bd1-8f63-24ad26d3144982b8be2d-dfa7-4bd1-8f63-24ad26d31449 [["8","d"],["d","7"],["4","1"],["8","3"],["2","9"]][["8","d"],["d","7"],["4","1"],["8","3"],["2","9"]]

使用命名捕获组Using named capture groups

可以在 extract_all() 中使用 RE2 的命名捕获组。You can use named capture groups of RE2 in extract_all(). captureGroups 使用捕获组索引和命名捕获组引用来提取匹配值。The captureGroups uses both capture group indexes and named capture group reference to fetch matching values.

print Id="82b8be2d-dfa7-4bd1-8f63-24ad26d31449"
| extend guid_bytes = extract_all(@"(?P<first>\w)(?P<middle>\w+)(?P<last>\w)", dynamic(['first',2,'last']), Id) 
IDID guid_bytesguid_bytes
82b8be2d-dfa7-4bd1-8f63-24ad26d3144982b8be2d-dfa7-4bd1-8f63-24ad26d31449 [["8","2b8be2","d"],["d","fa","7"],["4","bd","1"],["8","f6","3"],["2","4ad26d3144","9"]][["8","2b8be2","d"],["d","fa","7"],["4","bd","1"],["8","f6","3"],["2","4ad26d3144","9"]]