replace_object_body_regex
Replace specific sections of an object’s body using regex patterns to make precise edits without recreating the entire text. Ensure surgical text modifications with multiline matching, backreferences, and single-match enforcement for safety. Ideal for updating documentation, code snippets, or targeted content.
Instructions
Replaces portions of an object's body using regular expressions
Use this tool to make targeted edits to specific sections of an object's body content without recreating the entire text. This is safer and more efficient than wholesale body replacement for surgical text modifications.
The tool uses regex pattern matching with multiline and global capabilities, similar to Python's re.sub(). When allowMultipleOccurrences is false (default), it prevents unintended bulk replacements by throwing an error if multiple matches are found.
Regex Features:
- Multiline matching: patterns can span multiple lines
- Dot-all mode: . matches newlines
- Backreferences: use \1, \2, etc. in replacement text
- Case-sensitive matching (use (?i) flag in pattern for case-insensitive)
Common use cases:
- Update specific sections of documentation
- Modify code snippets within object descriptions
- Replace outdated information while preserving structure
- Update specific parameters or values
- Fix formatting or content errors in targeted areas
Safety features:
- Single-match enforcement (unless explicitly allowed)
- Pattern validation before execution
- Graceful error handling with detailed messages
- Original content preservation on pattern mismatch
Example patterns:
- Simple text: "old text" → "new text"
- With context: "(section:\s*)old content(\s*end)" → "\1new content\2"
- Multi-line: "BEGIN.*?END" → "BEGIN\nnew content\nEND"
- Backreferences: "(\w+) (\w+)" → "\2, \1"
The replacement preserves all object metadata and relationships while only modifying the body content that matches your pattern.
Input Schema
Name | Required | Description | Default |
---|---|---|---|
allowMultipleOccurrences | No | Allow replacing multiple pattern matches (defaults to false) | |
id | Yes | ID of the object whose body should be modified | |
regex | Yes | Regular expression pattern to match content in the body | |
replacement | Yes | Replacement text (may contain backreferences like \1, \2, etc.) |