# REGEXP_SUBSTR

> REGEXP_SUBSTR function in StackQL: extract the substring of an input that matches a regular expression.

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

Returns a substring from an input string matching a specified pattern.  

See also:  
[[` SELECT `]](/language-spec/select) [[ Regular Expression Reference ]](/language-spec/functions/re/reference) 

* * * 

## Syntax

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

## Arguments

__*source*__  
A literal string or string column from which you want to extract a substring based upon a pattern defined by a regular expression (`pattern`).  

__*pattern*__  
The regular expression pattern to search for.  

## Return Value(s)
Returns a string.

* * *

## Examples

### Extract a substring based upon a regular expression

**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';
```

**Results**

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

For more information, see [https://github.com/nalgeon/sqlean/blob/main/docs/re.md](https://github.com/nalgeon/sqlean/blob/main/docs/re.md)
