# Regex API (`regex`)

## `Regex` — Compiled Pattern

| Method | Description |
|--------|-------------|
| `Regex.new(pattern)` | Compile pattern with default flags |
| `Regex.compile(pattern, flags)` | Compile with explicit flags (`"i"`, `"m"`, `"s"`) |
| `Regex.invalid()` | Create an invalid/empty regex sentinel |
| `.test(text): bool` | Check if pattern matches anywhere in text |
| `.isMatch(text): bool` | Check if pattern matches the full text |
| `.exec(text, &match): bool` | Execute and capture groups |
| `.valid` | Check if compilation succeeded |
| `.source` | Original pattern string |
| `.groupCount` | Number of capture groups |

Invalid syntax, unknown flags or a pattern exceeding the documented resource
budgets returns `Regex.invalid()` (`valid == false`). Compilation does not print
to stdout.

## `Match` — Captured Result

| Method | Description |
|--------|-------------|
| `Match.new()` | Create empty match |
| `.value(): string` | Full match text (group 0) |
| `.group(i): Option<str>` | Get capture group by index |
| `.groupCount(): i32` | Number of capture groups (excluding group 0) |
| `.start(): i32` | Start position of full match |
| `.end(): i32` | End position of full match |

`Match` borrows the input text and owns reusable capture/scratch storage.
`group(i)` returns a borrowed `str`; keep the original input alive and unchanged
while using the match or its groups. Call `toString()` to retain an independent
copy. `value()` produces an owned `string`. `start()` and `end()` are byte
offsets (end-exclusive), with `-1` when no full-match span exists; they are not
Unicode-scalar or grapheme indexes.

Reuse one `Match` per concurrent caller; a later `exec` replaces its result.
The compiled `Regex` remains immutable during matching. The engine operates
on bytes, and case-insensitive matching is ASCII-oriented; use the Unicode
package for Unicode normalization/case-fold policy before matching.

## Flags

| Flag | Inline | Description |
|------|--------|-------------|
| `"i"` | `(?i)` | Case-insensitive matching |
| `"m"` | `(?m)` | Multiline (`^` and `$` match line boundaries) |
| `"s"` | `(?s)` | Dot-all (`.` matches `\n`) |

Only `i`, `m` and `s` are accepted by `Regex.compile`. Bare inline flags must
appear at the beginning of the pattern. Scoped groups such as `(?i:...)` are
currently rejected rather than being compiled with incorrect global semantics.

## Examples

```vex
import { Regex, Match } from "regex";

// Case-insensitive
let re = Regex.compile("hello", "i");
re.test("Hello World");  // true

// Capture groups
let re = Regex.new("(\\w+)@(\\w+)\\.(\\w+)");
let! m = Match.new();
if re.exec("user@example.com", &m!) {
    match m.group(1) {
        Some(user) => { $println(user); }, // user
        None => { $println("No capture"); },
    }
}

// Free functions
import { test, exec } from "regex";
test(&re, "some text");
```
