# REGEXP_SUBSTR

> StackQL 中的 REGEXP_SUBSTR 函式：擷取輸入中符合正規表示式的子字串。

Source: https://stackql.io/zh-hant/language-spec/functions/re/regexp_substr

從輸入字串傳回符合指定模式的子字串。  

另請參閱：  
[[` SELECT `]](/language-spec/select) [[ 正規表示式參考 ]](/language-spec/functions/re/reference) 

* * * 

## 語法

```sql
SELECT REGEXP_SUBSTR(source, pattern) FROM <multipartIdentifier>;
```

## 引數

__*source*__  
字串常值或字串資料行，您要依據由正規表示式（`pattern`）定義的模式從中擷取子字串。  

__*pattern*__  
要搜尋的正規表示式模式。  

## 傳回值
傳回字串。

* * *

## 範例

### 依據正規表示式擷取子字串

**StackQL**

```sql
select name,
json_extract(properties, '$.storageProfile.imageReference.offer') as os,
regexp_substr(
  json_extract(properties, '$.storageProfile.imageReference.exactVersion'), 
  '[0-9][0-9]\.[0-9][0-9]') as os_version 
from azure.compute.virtual_machines 
WHERE subscription_id = '273769f6-545f-45b2-8ab8-2f14ec5768dc';
```

**結果**

```
|------|---------------|------------|
| name |      os       | os_version |
|------|---------------|------------|
| vm0  | UbuntuServer  |      16.04 |
|------|---------------|------------|
| vm1  | UbuntuServer  |      16.04 |
|------|---------------|------------|
| vm2  | UbuntuServer  |      20.04 |
|------|---------------|------------|
```

如需詳細資訊，請參閱 [https://github.com/nalgeon/sqlean/blob/main/docs/re.md](https://github.com/nalgeon/sqlean/blob/main/docs/re.md)。
