{"record":{"id":"b0d68b15d0c18409","repo":"oraios/serena","slug":"error-no-matches-of-search-expression-found","errorCode":null,"errorMessage":"Error: No matches of search expression found.","messagePattern":"Error: No matches of search expression found\\.","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/serena/util/text_utils.py","lineNumber":474,"sourceCode":"        :return: the updated content after performing the replacement\n        \"\"\"\n        if self.mode == \"literal\":\n            regex = re.escape(needle)\n        elif self.mode == \"regex\":\n            regex = needle\n        else:\n            raise ValueError(f\"Invalid mode: '{self.mode}', expected 'literal' or 'regex'.\")\n\n        regex_flags = (re.MULTILINE | re.DOTALL) if self.regex_multiline else 0\n\n        # create replacement function with validation and backreference handling\n        repl_fn = self._create_replacement_function(regex, repl, regex_flags=regex_flags)\n\n        # perform replacement\n        updated_content, n = re.subn(regex, repl_fn, content, flags=regex_flags)\n\n        if n == 0:\n            raise ValueError(\"Error: No matches of search expression found.\")\n        if not self.allow_multiple_occurrences and n > 1:\n            raise ValueError(\n                f\"Expression matches {n} occurrences. \"\n                \"Please revise the expression to be more specific or enable allow_multiple_occurrences if this is expected.\"\n            )\n        return updated_content\n\n\n@dataclass\nclass ReplacementOccurrence:\n    \"\"\"A single prospective replacement of a pattern match within one file.\"\"\"\n\n    occurrence_id: str\n    \"\"\"stable, content-anchored identifier: '<relative_path>:<index_in_file>@<digest>'\"\"\"\n    relative_path: str\n    index_in_file: int\n    \"\"\"0-based index of this match among the matches within its file (in position order)\"\"\"\n    start: int","sourceCodeStart":456,"sourceCodeEnd":492,"githubUrl":"https://github.com/oraios/serena/blob/7fcbca7e62555ec2287ddb2f083caee805848ea6/src/serena/util/text_utils.py#L456-L492","documentation":"replace() uses re.subn and raises when zero matches were found for the search expression. Since these replacements are meant to be surgical edits, a zero-match result indicates the needle no longer matches the content, and the library fails loudly instead of no-op-ing.","triggerScenarios":"Calling replace() where the needle/regex does not occur in content at all — due to a typo, changed file content, or a regex escaping mistake.","commonSituations":"Library/framework upgrade changed the file text so templates no longer match; editing files whose formatting (whitespace, quotes) differs from the assumed pattern; regex specials like '(' not escaped in literal-mode confusion.","solutions":["Print/search the actual content to confirm the exact current text and update the needle","Escape regex metacharacters or switch to mode='literal'","Relax the regex (e.g. use .*? for variable whitespace) if formatting drifts","Check whether an earlier replacement in a chain already modified the target text"],"exampleFix":"// before\nreplacer.replace(content, 'def old_name(.*?', '...')  # function renamed\n// after\nif re.search(r'def old_name\\(', content):\n    ...\nelse:\n    content = replacer.replace(content, r'def \\w+\\(', '...')  # flexible pattern","handlingStrategy":"validation","validationCode":"if re.search(regex, content, flags=re.MULTILINE|re.DOTALL) is None:\n    raise LookupError('needle not present; skipping replacement')","typeGuard":null,"tryCatchPattern":"try:\n    updated = editor.replace(content, needle, repl)\nexcept ValueError as e:\n    if 'No matches' in str(e):\n        logging.warning('needle not found; content may have changed')\n        updated = content\n    else:\n        raise","preventionTips":["Search the live content for the exact text before replacing","Use literal mode or escape metacharacters","Allow flexible whitespace with \\\\s* / .*? to survive reformatting","Pin/pattern against the file version you target"],"tags":["regex","text-processing","no-match"],"backgroundTag":"pattern-not-found","analyzedSha":"7fcbca7e62555ec2287ddb2f083caee805848ea6","analyzedAt":"2026-08-29T00:04:09.619Z","schemaVersion":2},"datasetVersion":"2026-08-29T02:17:18.158Z"}