# IndexRegEx 指令碼與圖表函數

IndexRegEx() 會搜尋輸入字串並傳回指定規則運算式模式第 n 次出現的起始位置。可選的第三個引數 count 會提供 n 的值，如果省略則為 1。字串中的位置編號是由左至右從 1 算起。如果未找到相符項，則該函數會傳回 0。

此函數執行區分大小寫的 Regex 操作。您也可以使用變體 IndexRegExI() 來執行不區分大小寫的 Regex 操作。

語法：

IndexRegEx (text, regex [, count])

傳回的資料類型： 整數

引數

| 引數 | 描述 |
| --- | --- |
| text | 要在其中搜尋規則運算式的輸入字串文字。 |
| regex | 用於搜尋輸入字串的規則運算式。 |
| count | 相符項的編號。當可能在文字中找到規則運算式的多個相符項時，這非常有用。例如，指定值 `4` 以擷取第四個相符項的位置。<br><br>此為選用引數。如果未指定，則預設為 1。您可以指定負值以從右到左搜尋相符項。 |

函數範例

| 範例 | 結果 |
| --- | --- |
| `IndexRegEx('abc123','[a-z][0-9]+')` | 傳回 `3` (第一個相符項的起始位置)。 |
| `IndexRegEx('abc123','[a-z][0-9]+',2)` | 傳回 `0` (Regex 沒有第二個相符項)。 |
| `IndexRegEx('ABC123','[a-z][0-9]+')` | 傳回 `0`，因為 IndexRegEx() 區分大小寫。 |
| `IndexRegExI('ABC123','[a-z][0-9]+')` | 傳回 `3`。使用不區分大小寫的函數變體 IndexRegExI()。 |

## 什麼情況下使用

此函數的使用案例包括：

* 辨識特定文字模式在較大文字內文中出現的位置。例如，您可能想要知道電子郵件地址模式在一系列長電子郵件中的使用位置。

* IndexRegEx() 特別適合用於進階資料處理，並且通常在更長、更複雜的轉換的第一步中使用。使用其他規則運算式函數 (例如 ExtractRegEx()、MatchRegEx() 和 CountRegEx()) 通常更容易解決問題，但有時 IndexRegEx() 可以提供這些函數無法提供的解決方案。

## 範例 1 – 用於透過 ISBN 識別書籍的載入指令碼

[](https://help.qlik.com/en-US/sense/Subsystems/Hub/Content/Sense_Hub/Scripting/StringFunctions/IndexRegEx.htm)

#### 載入指令碼和結果

概述

開啟資料載入編輯器並將下面的載入指令碼新增至新的區段。

載入指令碼包含：

* 表格 `ISBN` 在名為 `EmailBody` 的欄位中包含電子郵件內容。

我們想要從電子郵件的自然語言中找到所有 13 位 ISBN 代碼的位置。

載入指令碼

```
SET ISBN_RegEx = 'ISBN[ ]*([0-9]{3})-([0-9]{1})-([0-9]{4})-([0-9]{4})-([0-9]{1})';

ISBN:
LOAD IndexRegEx(EmailText,'$(ISBN_RegEx)',1) AS ISBNCode1,
IndexRegEx(EmailText,'$(ISBN_RegEx)',2) AS ISBNCode2,
IndexRegEx(EmailText,'$(ISBN_RegEx)',3) AS ISBNCode3,
* INLINE `
EmailText
Hi there! I have some books that I'm interested in ordering from your distribution center. Just wanted to make sure they were in stock before I put the order in on the system - do you think you could tell me whether you have these in stock, and how many you might be able to ship us? Thanks! Item 1: ISBN 123-3-1234-1234-0. This is one I've had a really hard time getting a hold of lately. Do you think you would have 5 in stock? If not, can I put a hold on the next 5 you get? Item 2: ISBN 012-2-0123-0123-4 ... This one is not high-priority, but if you have one, I'll take it! Item 3: ISBN 000-1-0123-0123-2. Customers have been requesting this one for a long time, and we haven't had it in stock for years due to it being out of print. Any chance you might have 7 of them? Thanks!
` (delimiter is '/t/';
```

結果

載入資料並開啟工作表。建立新的表格並將這些欄位新增為維度：

* `ISBNCode1`

* `ISBNCode2`

* `ISBNCode3`

結果表格

| ISBNCode1 | ISBNCode2 | ISBNCode3 |
| --- | --- | --- |
| 301 | 487 | 588 |

## 範例 2 – 用於識別具有次要電話號碼之聯絡人的圖表運算式

[](https://help.qlik.com/en-US/sense/Subsystems/Hub/Content/Sense_Hub/Scripting/StringFunctions/IndexRegEx.htm)

#### 圖表運算式和結果

概述

開啟資料載入編輯器並將下面的載入指令碼新增至新的區段。

載入指令碼包含：

* 名為 `BusinessContactInfo` 的表格，其中包含在每個公司網站上找到的公司相關資訊。

* `ContactInfo` 是包含公司電話號碼的欄位。我們想要確定哪些公司有多個可用電話號碼。

我們的要求：

* 電話號碼必須是 10 位 NANP 電話號碼。

* 我們希望允許將區號括在方括弧中。

* 我們希望在電話號碼的每個部分之間留一個空格或連字符。

載入指令碼

```
BusinessContactInfo:

Load * Inline `
ID	CompanyName	ContactInfo
1	Company A	(123) 456-7890 (023) 123-4567
2	Company B	0123456790 1357913579 0246802468
3	Company C	234-567-8901

` (delimiter is '\t');
```

結果

載入資料並開啟工作表。建立新的表格並將此欄位新增為維度：

* `CompanyName`

將此計算維度新增至表格：

```
=if(IndexRegEx(ContactInfo, '\({0,1}[0-9]{3}\){0,1}[ -]{0,1}[0-9]{3}[ -]{0,1}[0-9]{4}', 2)=0, 'No secondary phone number(s)', 'Has secondary phone number(s)')
```

結果表格

| CompanyName | =if(IndexRegEx(ContactInfo, '\({0,1}[0-9]{3}\){0,1}[ -]{0,1}[0-9]{3}[ -]{0,1}[0-9]{4}', 2)=0, 'No secondary phone number(s)', 'Has secondary phone number(s)') |
| --- | --- |
| Company A | Has secondary phone number(s) |
| Company B | Has secondary phone number(s) |
| Company C | No secondary phone number(s) |

`Company A` 有一個次要電話號碼。`Company B` 有兩通次要電話號碼。`Company C` 有三個次要電話號碼。

資訊備註MatchRegEx() 函數也很適合這些類型的驗證使用案例。

## 瞭解更多資訊

* [在載入指令碼和圖表運算式中使用規則運算式](https://help.qlik.com/zh-TW/sense/May2026/Subsystems/Hub/Content/Sense_Hub/Scripting/regular-expressions-qlik-analytics.htm)