# Module /regex

Compiled regular expressions (Regex) with matching, capture extraction, replacement, and splitting.

```tangu
(use /regex)
```

## Functions & symbols

### Regex

`(Regex pattern) -> Regex`

Compiles a regular expression `pattern` into a reusable `Regex` value.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `pattern` | `String` | no | Regular expression pattern string |

```tangu
(let re (regex/Regex "^v(\\d+)\\.(\\d+)"))
```

### capture

`(capture re text) -> (Or String None)`

Returns the first full match substring in `text`, or `()` if none.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `re` | `Regex | String` | no | Compiled Regex or pattern |
| `text` | `String` | no | Input text |

```tangu
(regex/capture "[0-9]+" "port=8080") ; -> "8080"
```

### captures

`(captures re text) -> (Or Map Array None)`

Extracts positional and/or named capture groups from the first match of `re` in `text`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `re` | `Regex | String` | no | Compiled Regex or pattern |
| `text` | `String` | no | Input text |

```tangu
(regex/captures "(?<k>\\w+)=(?<v>\\d+)" "port=8080")
```

### captures-all

`(captures-all re text) -> Array`

Returns all successive non-overlapping capture matches in `text` as an Array.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `re` | `Regex | String` | no | Compiled Regex or pattern |
| `text` | `String` | no | Input text |

```tangu
(regex/captures-all "\\w+" "a b c")
```

### is-matching?

`(is-matching? re text) -> Bool`

Returns `true` if `re` matches anywhere inside `text`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `re` | `Regex | String` | no | Compiled Regex or pattern string |
| `text` | `String` | no | Input text to test |

```tangu
(regex/is-matching? "\\d+" "id-42") ; -> true
```

### matching?

`(matching? re text) -> Bool`

Alias of `is-matching?`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `re` | `Regex | String` | no | Compiled Regex or pattern |
| `text` | `String` | no | Input text |

```tangu
(regex/matching? "^[a-z]+$" "tangu")
```

### replace

`(replace text re replacement) -> String`

Replaces all matches of `re` in `text` with `replacement`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `text` | `String` | no | Input text |
| `re` | `Regex | String` | no | Regex or pattern to replace |
| `replacement` | `String` | no | Replacement string (supports $1, $name backreferences) |

```tangu
(regex/replace "2026-05-01" "-" "/") ; -> "2026/05/01"
```

### split

`(split sep-re text) -> (Array String)`

Splits `text` around occurrences matching `sep-re`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `sep_re` | `Regex | String` | no | Separator Regex or pattern |
| `text` | `String` | no | Input text to split |

```tangu
(regex/split "[,\\s]+" "a, b  c")
```

### split-matches

`(split-matches match-re text) -> (Array String)`

Splits `text` while alternating matched substrings and unmatched slices.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `match_re` | `Regex | String` | no | Regex matching tokens to preserve alongside delimiters |
| `text` | `String` | no | Input text |

```tangu
(regex/split-matches "\\d+" "a1b2c")
```
