replace_in_file — regex replace in a single file
replace_in_file runs re.subn(pattern, replacement, contents) over a single file and writes the result back. Unlike edit_file, it tolerates multiple matches; unlike apply_patch, it works on one file at a time.
Schema
Section titled “Schema”| Arg | Type | Required | Description |
|---|---|---|---|
path | string | yes | Relative or absolute path. The file must exist. |
pattern | string | yes | Regex pattern (Python re flavor). |
replacement | string | yes | Replacement string. Supports \1, \2, … backreferences. |
Example invocation
Section titled “Example invocation”{ "path": "src/app.py", "pattern": "DEBUG\\s*=\\s*True", "replacement": "DEBUG = False"}from chimera.tools.replace_in_file import ReplaceInFileTool
tool = ReplaceInFileTool()result = tool.execute( {"path": "docs/changelog.md", "pattern": r"v(\d+)\.(\d+)\.0", "replacement": r"v\1.\2.1"}, env=local_env,)Output sample
Section titled “Output sample”3 replacements made in docs/changelog.mdIf the pattern matches nothing:
0 replacements made in docs/changelog.md(no error — count == 0 is just reported).
See also
Section titled “See also”edit_file— exact-string, single-occurrence variant.apply_patch— multi-file atomic edits.