[BUG] Regex replace in the note search widget ignores context (lookbehind/lookahead don't work)
In regular expression mode, "Replace"/"Replace all" in the note text search
widget re-applies the regex to the selected text only
(QTextEditSearchWidget::doReplace, src/widgets/qtexteditsearchwidget.cpp):
QString text = c.selectedText();
text.replace(QRegularExpression(ui->searchLineEdit->text()), ui->replaceLineEdit->text());
c.insertText(text);
The search finds a match using the whole document, but the replacement is computed on the isolated match. Anything that depends on the surrounding text no longer matches, so the replacement is silently skipped or wrong.
Steps to reproduce (verified on v26.10.12)
- Note text:
ab - Search (regex mode):
(?<=a)b, replace with:X - Click "Replace" / "Replace all"
Expected: aX
Actual: ab (the match is found and selected, but the replace does nothing)
The same applies to (?=...), (?<!...) and \B.
Possible fix
Compute the replacement from the real match instead of the selection: run
QRegularExpression::match() on the document text starting at the selection
start (with AnchoredMatchOption), so lookarounds see the context. Then expand
\1..\9 from the captured() groups, and replace the selection with the result.
Related: QTextDocument::find() with a regex works per block, so
patterns spanning several lines cannot match (likely, untested).
Happy to submit a PR if the approach is OK.
Fixed regular expression replacements losing surrounding text context, so lookarounds and word-boundary checks work correctly, while preserving capture references and case sensitivity.
There now is a new release, could you please test it and report if it works for you?
Damn, you're quick! Thanks, 26.11.2 works for me! 👍
Great, thank you for testing! 😊