[Geany] Regex replace weirdness
Lex Trotman
elextr at xxxxx
Wed Sep 29 02:41:35 UTC 2010
On 29 September 2010 11:34, Colomban Wendling
<lists.ban at herbesfolles.org> wrote:
> Le 29/09/2010 03:31, Lex Trotman a écrit :
>> On 29 September 2010 11:24, Colomban Wendling
>> <lists.ban at herbesfolles.org> wrote:
>>> Le 29/09/2010 03:16, Lex Trotman a écrit :
>>>> On 29 September 2010 11:09, Lex Trotman <elextr at gmail.com> wrote:
>>>>> On 29 September 2010 10:30, Erik de Castro Lopo <mle+tools at mega-nerd.com> wrote:
>>>>>> Hi all,
>>>>>>
>>>>>> I've got a file of C code which for part of the file has an extra
>>>>>> single space indent on the left hand side. To fix this I'm trying
>>>>>> to do a regex search for "^ " and replace it with "" (ie empty
>>>>>> string). Unfortunately, this search/replace removes all space
>>>>>> indentation which is not really what I expect.
>>>>>>
>>>>>> Any clues on getting this right? Is this a bug?
>>>>
>>>> Actually having a bit of a think, it can't work since after replacing
>>>> the first space with nothing it will find another space at the same
>>>> place and remove it until it removes all the spaces at the start of
>>>> the line before moving on to the next.
>>>>
>>>> So its working right, just not what you want :-) & I'm not sure how to do it.
>>> not sure it is "right" -- it already matched ^, so why match it again?
>>
>> Because it has removed the space it matched, the so its position is
>> back to the start of the line (which matches ^).
> Yes I agree it is a possible understanding of the thing, but I'm not
> sure it is "the one".
>
> BTW sed behaves the way Erik wants:
> sed 's/^ //g' file
> will only remove the first spaces of every lines, even though the option
> g is present.
Yes because sed only operates on a line at a time and g finds all the
matches in the line then makes the substitutions, it does not run the
substitution multiple times.
Geany does not find all the locations first and then do the replaces,
it does find, replace, go to the end of the replacement, repeat until
done
The REGEX rules (Perl and POSIX which is pretty canonical :-) require
left most longest greedy match, ie the ^ assertion must be tested at
the current position first. To do anything else would be non-standard
behavior. The REGEX engine doesn't know it has just previously done a
replacement with nothing at the current position.
So I would say that the current behavior IS correct, and since
Columban has given us an incantation to cover those rare situations
where the standard behavior is annoying, there is no need for some
non-standard special case to be added.
The only possible change might be to re-phrase "replace all" in the
manual and dialog to be "repeat replacement".
Cheers
Lex
>
> Regards,
> Colomban
> _______________________________________________
> Geany mailing list
> Geany at uvena.de
> http://lists.uvena.de/cgi-bin/mailman/listinfo/geany
>
More information about the Users
mailing list