---
title: Regex
description: Search and replace text with regular expressions
---
```bpl
]help •r
```

# `•r` — Regular expressions

`text •r pattern` gives a table of the matches of the regular expression `pattern` in `text`, as a record of columns: `text`, the matched text; `position`, its position in characters from 0; and `groups`, the text of each group, which is empty for a group that took no part. [`•replace`](system/replace.md) replaces matches. BPL keeps the 16 patterns it compiled last, so a loop that uses one pattern compiles it once.

Natively the pattern uses [fancy-regex's syntax](https://docs.rs/fancy-regex/latest/fancy_regex/#syntax), including lookaround and backreferences. Put flags in the pattern, as in `(?i)`. A search that exceeds the engine's backtracking limit raises `DOMAIN`. In the browser the pattern is a JavaScript regular expression, with [JavaScript's syntax](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions). [Regular expressions](regex.md) covers matching and replacing.

```bpl
"ab12 cd3" •r "([a-z]+)([0-9]+)"   ⍝ ["text":["ab12" "cd3"] "position":[0 5]ₓ "groups":("ab" "12"◇"cd" "3")]
```

Errors: `DOMAIN` for invalid patterns and non-text arguments; `SYNTAX` for the wrong valence; `LIMIT` for oversized results.

<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

Natively, patterns use [fancy-regex syntax](https://docs.rs/fancy-regex/latest/fancy_regex/#syntax), including lookaround and backreferences. Exceeding the engine's backtracking limit raises `DOMAIN`. In the browser, patterns use [JavaScript's regular expression syntax](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions). `text •r pattern` gives a table of the matches, as a [record](terms.md#term-record) of columns. This pattern finds runs of digits:

```bpl
"abc123def45" •r "[0-9]+"
```

```output
["text":["123" "45"] "position":[3 9]ₓ "groups":[0ρ⊂"" 0ρ⊂""]]
```

Matches don't overlap. The `position` column counts characters, and `≢¨` of the `text` column gives each match's length. For a text with no match, `•r` gives a table with no rows:

```bpl
m←"aé🐈🐈z" •r "é|🐈+"
m.position
≢¨m.text
"abc" •r "é|🐈+"
```

```output
[1 2]ₓ
[1 2]ₓ
["text":0ρ⊂"" "position":⍬ₓ "groups":0ρ⊂0ρ⊂""]
```

The `groups` column holds the captured strings of each match, without the whole match. For an unmatched optional capture, `groups` holds `""`:

```bpl
("ab12 cd3" •r "([a-z]+)([0-9]+)").groups
```

```output
("ab" "12"◇"cd" "3")
```

`text •replace [pattern template]` replaces each match of `pattern` with `template`. In a native template, `$0` is the whole match and `$$` is a literal `$`. An unknown or unmatched capture expands to `""`. In the browser, templates use JavaScript's syntax, where `$<word>` refers to the group named `word`:

```bpl
"ab12" •replace ["(?P<word>[a-z]+)([0-9]+)" "${word}:$2:$$"]
```

```output
"ab:12:$"
```

Natively, inline flags change how a pattern matches. `(?i)` ignores case. `(?m)` makes `^` and `$` match at each line break. `(?s)` makes [`.`](https://bpl.fast.ai//glyphs/dot.html) match line breaks too. Use [`¨`](https://bpl.fast.ai//glyphs/diaeresis.html) to search several strings:

```bpl
{≢(ω •r "(?i)cat").text}¨"Cat cat" "dog"
```

```output
[2 0]ₓ
```

Empty matches occur at character boundaries:

```bpl
("é🐈" •r "").position
"é🐈" •replace ["" "-"]
```

```output
[0 1 2]ₓ
"-é-🐈-"
```

`lib/regex.bpl` supplies `pattern f regex_replace text`. It applies `f` to each matched string and keeps the text between matches:

```bpl
[regex_replace:]←•load "../lib/regex.bpl"
"[0-9]+" {⍕⊽•jsonω}regex_replace "a12 b3"
```

```output
"a24ₓ b6ₓ"
```
