# regex/v1 — Extract, replace, split

Regex extract/replace/split, using Go's standard library `regexp` package.
`regexp` is RE2-based: matching is linear-time in input length with no
catastrophic-backtracking risk, at the cost of not supporting
backreferences (`\1` inside the pattern) or lookaround (`(?=...)`).

This is a separate concern from `POST /v1/validate/regex`, which only
reports whether a pattern matches at all.

### `POST /v1/regex/extract`

**Request**

```json
{ "pattern": "(?P<id>[0-9]+)", "input": "ticket-123", "maxMatches": 1 }
```

- `pattern` (required) — an RE2 pattern; see [Go's `regexp/syntax`](https://pkg.go.dev/regexp/syntax) for the full grammar. Named groups use `(?P<name>...)`.
- `input` (required) — text to search.
- `maxMatches` (optional) — stop after this many matches; omitted or `<= 0` means no limit, capped at 10,000 matches server-side regardless.

**Response**

```json
{
  "matches": [
    {
      "match": "123",
      "start": 7,
      "end": 10,
      "groups": ["123"],
      "namedGroups": { "id": "123" }
    }
  ]
}
```

`start`/`end` are byte offsets into `input`. `groups` holds every capture
group (positional, index 1+) in order, `""` for a group that didn't
participate in the match; `namedGroups` additionally maps the subset of
groups given a `(?P<name>...)` name.

### `POST /v1/regex/replace`

**Request**

```json
{ "pattern": "(\\w+)@(\\w+)", "input": "user@host", "replacement": "$2@$1" }
```

**Response**

```json
{ "output": "host@user" }
```

`replacement` uses [`Regexp.Expand`](https://pkg.go.dev/regexp#Regexp.Expand) syntax: `$1`, `${1}`, `$name` reference capture groups; `$$` is a literal `$`. All matches are replaced — there is no match-count limit on `/replace`.

### `POST /v1/regex/split`

**Request**

```json
{ "pattern": ",\\s*", "input": "a, b,c,  d" }
```

**Response**

```json
{ "parts": ["a", "b", "c", "d"] }
```

`limit` (optional) caps the number of returned substrings — the last one
contains the unsplit remainder of the input. Omitted or `<= 0` means no
limit.

**Error response**

```json
{ "error": "invalid pattern: error parsing regexp: missing closing ): `(unclosed`" }
```

## Out of scope

Backreferences and lookaround (not supported by RE2, and PCRE-compatible
matching brings back catastrophic-backtracking risk), and match-count
limits on `/replace` — ask if you need one added.

## Files

- `regex.go` — module wiring (routes, HTTP handlers)
- `ops.go` — pure extract/replace/split logic
- `ops_test.go` — known-vector and error tests
