從來源字串取得正則表達式的相符專案。
或者,將擷取的子字串轉換成指定的類型。
語法
extract(
,
深入瞭解 語法慣例。
參數
| 姓名 | 類型 | 必要 | 描述 |
|---|---|---|---|
| regex | string |
✔️ | 正 則表達式。 |
| captureGroup | int |
✔️ | 要擷取的擷取群組。 0 代表整個比對,1 代表正則表達式中第一個 '('括弧')' 所比對的值,2 或更多代表後續括弧。 |
| 源 | string |
✔️ | 要搜尋的字串。 |
| typeLiteral | string |
若提供了此數值,擷取的 substring 將會轉換為此型別。 例如: typeof(long) 。 |
傳回
如果 regex 在來源中尋找相符專案:與指定的擷取群組 captureGroup 相符的子字串,則選擇性地轉換成 typeLiteral。
如果沒有相符專案,或類型轉換失敗: null。
範例
下列範例會從字串中擷取使用者名稱、電子郵件、年齡。 正 則表達式 可用來擷取資訊。
let _data = datatable(Text: string)
[
"User: James, Email: James@example.com, Age: 29",
"User: David, Age: 35"
];
_data |
extend UserName = extract("User: ([^,]+)", 1, Text),
EmailId = extract(@"Email: (\S+),", 1, Text),
Age = extract(@"\d+", 0, Text)
輸出
| 文字 | UserName |
EmailId |
Age |
|---|---|---|---|
| 用戶:James,電子郵件: James@example.com,年齡:29 | 詹姆斯 | James@example.com | 二十九 |
| 用戶:大衛,年齡:35 歲 | David | 35 |
下列範例會從字串擷取月份,並傳回具有日期字串 Dates 和月份做為 int 類型的數據表。
let Dates = datatable(DateString: string)
[
"15-12-2024",
"21-07-2023",
"10-03-2022"
];
Dates
| extend Month = extract(@"-(\d{2})-", 1, DateString, typeof(int))
| project DateString, Month
輸出
| DateString | Month |
|---|---|
| 15-12-2024 | 12 |
| 21-07-2023 | 7 |
| 10-03-2022 | 3 |