Bug 142598

Summary: Regular expressions containing ^ or $ should be matched only once per line
Product: [Applications] kate Reporter: Christoph Bartoschek <bartoschek>
Component: generalAssignee: KWrite Developers <kwrite-bugs-null>
Status: RESOLVED FIXED    
Severity: normal CC: groszdanielpub, waqar.17a
Priority: NOR    
Version First Reported In: unspecified   
Target Milestone: ---   
Platform: openSUSE   
OS: Linux   
Latest Commit: Version Fixed/Implemented In:
Sentry Crash Report:

Description Christoph Bartoschek 2007-03-06 15:14:05 UTC
Version:            (using KDE KDE 3.5.6)
Installed from:    SuSE RPMs

Today I failed to remove the first space (and only the first space) from all lines containing the space.

Given a file with the following contents (replace <space> by the space character)

<space>a
<space><space>b
<space><space><space>c

I wanted to get:

a
<space>b
<space><space>c

I've used the regular expression "^ " in the find & replace dialog and "" as the new text. Kate removed now all leading spaces. The result was:

a 
b
c

It seems as if kate reevaluates the same line again and again till there is no match and this leads to the visible result. 

In my opinion the search for the next match should start after the last one in a global search and replace as this is done for all normal searches and this means that the beginning of the current line is no longer included in the search.
Comment 1 Sebastian Pipping 2007-08-31 00:30:30 UTC
This sounds like a duplicate of another repeated-replacing bug. Also since searching was re-implemented in between I think this bug can be closed.
Comment 2 Grósz Dániel 2021-06-11 03:32:31 UTC
I don't know if this really has been fixed at some point, but it has the broken (or at least unexpected) behavior again.

It also happens with other assertions: if e.g. you replace "(?<= )." with "", it removes everything after the first space, rather than only the first character after each space; likewise, if you replace "\b\w" with "", it removes every word character, rather than just the first character of each word.

My guess as to why this happens is that in each replacement step, Kate first preforms a replacement, then moves the cursor to the end of the replacement text (which is empty in our examples), and then performs the next search beginning from there.

Instead, when using Replace All, it should first find all instances to replace, and then perform all the replacements. This is what other regex replacement engines seem to do, such as those of sed and javascript (at least in effect; I don't know how they are implemented). When using the Replace button, it should probably take into account the result of previous replacement, but not the current replacement, when finding the next occurrence of the search string.
Comment 3 Waqar Ahmed 2022-02-15 16:58:38 UTC
Not reproducible with latest master. For new bugs => new reports. Lets let the old ones rest.
Comment 4 Grósz Dániel 2022-06-27 01:03:38 UTC
(In reply to Waqar Ahmed from comment #3)
> Not reproducible with latest master. For new bugs => new reports. Lets let
> the old ones rest.

Still reproducible with 22.04.2 as well as the latest master (da4b519d2). I opened a new bug report (Bug 455995).