QOwnNotes icon indicating copy to clipboard operation
QOwnNotes copied to clipboard

[BUG] Regex replace in the note search widget ignores context (lookbehind/lookahead don't work)

Open klangme1ster opened this issue 1 month ago • 4 comments

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)

  1. Note text: ab
  2. Search (regex mode): (?<=a)b, replace with: X
  3. 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.

klangme1ster avatar Nov 01 '26 11:11 klangme1ster

Fixed regular expression replacements losing surrounding text context, so lookarounds and word-boundary checks work correctly, while preserving capture references and case sensitivity.

pbek avatar Nov 01 '26 14:11 pbek

There now is a new release, could you please test it and report if it works for you?

pbek avatar Nov 01 '26 15:11 pbek

Damn, you're quick! Thanks, 26.11.2 works for me! 👍

klangme1ster avatar Nov 01 '26 15:11 klangme1ster

Great, thank you for testing! 😊

pbek avatar Nov 01 '26 17:11 pbek