Bug 449545

Summary: Replace in "Search and Replace" does not work when the search regex contains lookahead or lookbehind part
Product: [Applications] kate Reporter: Atanas Uzunov <atanasuzunov>
Component: partAssignee: KWrite Developers <kwrite-bugs-null>
Status: RESOLVED FIXED    
Severity: major CC: kare.sars
Priority: NOR    
Version First Reported In: 21.12.0   
Target Milestone: ---   
Platform: unspecified   
OS: All   
Latest Commit: Version Fixed/Implemented In: 22.04
Sentry Crash Report:

Description Atanas Uzunov 2022-02-03 08:40:56 UTC
SUMMARY
When the search string entered in the "Search and Replace" plugin is a regular expression with lookahead or lookbehind part, the replace functionality does not work.

STEPS TO REPRODUCE
1. Open Kate and write a simple text in the editor, for ex:
SRC0
SRC1
SRC2

2. Open Search and Replace, enable regular expression search and type the following regex in the search field:
SRC(?=\d)

3. Type any replace string in the replace field, for ex:
DST

3. Press Search. The found lines will display fine.

4. Press Replace or Replace Checked

OBSERVED RESULT
The found "SRC" strings are not replaced with "DST"

EXPECTED RESULT
The found "SRC" strings should be replaced with "DST"

SOFTWARE/OS VERSIONS
Observed in Kate 21.12.0 for Windows, as well as Kate in the unstable branch of KDE Neon.
Qt Version: 5.15.2 (for the Windows version)

ADDITIONAL INFORMATION
In KWrite (in KDE Neon unstable branch), the above steps work fine.
Comment 1 Kåre Särs 2022-02-05 19:24:42 UTC
I can confirm this bug.
Comment 2 Bug Janitor Service 2022-02-06 17:20:57 UTC
A possibly relevant merge request was started @ https://invent.kde.org/utilities/kate/-/merge_requests/587
Comment 3 Kåre Särs 2022-02-10 05:43:40 UTC
Git commit cb759994beb24b119492e588147be1916390d017 by Kåre Särs.
Committed on 09/02/2022 at 20:14.
Pushed by sars into branch 'master'.

S&R: Fix Look-ahead & behind highlighting & replace

Strip the look-ahead and look-behind from the regular-expression that
checks if a string has been modified after search and returns the
possible captures. We strip the look-ahead/behind because we only
match against the previously found range.

M  +27   -1    addons/search/MatchModel.cpp
M  +6    -0    addons/search/MatchModel.h
A  +48   -0    addons/search/autotest/input/LookaheadLookbehindTestFile.txt
A  +22   -0    addons/search/autotest/input/MultilineStartEndOfLineMatch.txt
M  +2    -9    addons/search/plugin_search.cpp

https://invent.kde.org/utilities/kate/commit/cb759994beb24b119492e588147be1916390d017