# Regex replace

Replace every match of a regular expression, with backreferences such as \1 in the replacement.

- Endpoint: `POST https://setget.net/v1/tools/regex-replace` with JSON `{"input": {...}}`
- MCP: `run_tool` with `slug` = `regex-replace`
- Page: https://setget.net/tools/regex-replace

## Inputs

- `pattern` (required, string): The regular expression (Python syntax).
- `text` (required, string): The text to change.
- `replacement` (optional, string, default `""`): Replacement text. \1 or \g<name> inserts a group, \g<0> the whole match.
- `flags` (optional, string, default `""`): Letters that switch options on: i (ignore case), m (^ and $ match at each line), s (dot matches line breaks), x (allow spaces and comments).
- `count` (optional, integer, default `0`, range 0..): Replace at most this many matches. 0 replaces all.

## Example 1

```json
{"input":{"pattern":"(\\w+)@(\\w+)\\.com","text":"sam@example.com","replacement":"\\2: \\1"}}
```

Result:

```json
{"text":"example: sam","replacements":1}
```

## Example 2: Collapse whitespace

```json
{"input":{"pattern":"\\s+","text":"a   b \n c","replacement":" "}}
```

Result:

```json
{"text":"a b c","replacements":2}
```

**Which regex syntax is this?** Python's. It is close to PCRE and JavaScript, with named groups written (?P<name>...).

**Can I write $1 for a group?** No. Use \1, or \g<1> when a digit follows, and \g<name> for named groups. A dollar sign is inserted as it is.

**How do I insert a real backslash?** Write two backslashes in the replacement.
