> ## Documentation Index
> Fetch the complete documentation index at: https://docs.rebind.gg/llms.txt
> Use this file to discover all available pages before exploring further.

# rb.regex

> Match, find, replace, and split text with regular expressions.

`rb.regex` matches, finds, replaces and splits text by pattern. A script reaches
for it to pull a number or a name out of a line of text, or to rewrite text
where a plain string search is not enough.

This script reads the gold amount from a line, lists every name and number in
it, and rewrites it:

```luau theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
-- rebind: min_sdk=3.9.0 name=example version=1.0.0
local line = "Gold: 1500g  Wood: 320  Stone: 75"

local gold = rb.regex.find(line, [[Gold: (\d+)g]])
if gold then
  print("gold", tonumber(gold.captures[1]), "at byte", gold.start)
end

for _, m in ipairs(rb.regex.find_all(line, [[(\w+): (\d+)]])) do
  print(m.captures[1], m.captures[2])
end

print(rb.regex.replace_all(line, [[(\w+): (\d+)g?]], "$1=$2"))  -- Gold=1500  Wood=320  Stone=75
```

Patterns use Rust `regex` syntax: no lookaround or backreferences. Write
patterns as long strings, such as `[[\d+]]`, to avoid Luau escapes. An invalid
pattern raises `invalid regex '<pattern>': <reason>`.

| Signature | Returns |
| - | - |
| `rb.regex.is_match(text: string, pattern: string) -> boolean` | |
| `rb.regex.find(text: string, pattern: string) -> RegexMatch?` | the first match |
| `rb.regex.find_all(text: string, pattern: string) -> { RegexMatch }` | every match |
| `rb.regex.replace(text: string, pattern: string, replacement: string) -> string` | first match replaced |
| `rb.regex.replace_all(text: string, pattern: string, replacement: string) -> string` | every match replaced |
| `rb.regex.split(text: string, pattern: string) -> { string }` | |

A match is `{ match, captures, start, finish }`: `captures` lists the groups and
`start`, `finish` are 1-based byte offsets. Replacements refer to groups as `$1`,
`$2`.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.