[Gambas-user] RegExp.Replace() question

Benoît Minisini gambas at ...1...
Sat Apr 29 20:43:32 CEST 2017


Le 29/04/2017 à 16:30, Tobias Boege a écrit :
> On Sat, 29 Apr 2017, Benoît Minisini wrote:
>> Le 29/04/2017 à 13:48, Tobias Boege a écrit :
>>>
>>> Yes, admittedly it is strange behaviour of RegExp.Replace() to invert
>>> the greediness of all quantifiers and it was discovered just a few days
>>> ago. I don't know if it is going to be fixed though. I bet there was a
>>> rationale behind this setting, but we'll have to wait for a comment of
>>> Benoit who wrote the code.
>>>
>>
>> I have no idea about why I forced the PCRE_UNGREEDY flag. :-/
>>
>> Maybe to match the behaviour of the Gambas Replace() function.
>>
>
> Hmm, I know I was involved in adding the Match operator and Replace() method,
> at least mentally, so I must have read and liked the code, too. I guess it
> looked like the saner default for substitutions to be ungreedy. The greedy
> and ungreedy modifiers in PCRE were something I probably didn't have in mind
> at that time.
>
>> The only thing I can do is the following: when specifying explicitly the
>> RegExp.Ungreedy constant in the RegExp.Replace() options, then do the
>> contrary (remove the ungreedy flag).
>>
>> What do you think about that?
>>
>
> That's a straightforward fix but makes the code even more confusing:
>
>   RegExp.Replace(str, "#*", "", RegExp.Ungreedy)
>
> would greedily replace strings of "#". Maybe it is more explicit to add
> another constant entirely, like RegExp.ReplaceGreedy which does the same
> as you proposed but has a matching name. The documentation should continue
> to mention that the default behaviour of Replace() is to invert greediness
> and IMHO it should advise not to use the ungreedy modifier "?" if possible
> to avoid confusion.
>
> Any other opinions?
>
> Regards,
> Tobi
>

Done in revision #8131. The RegExp.Greedy constant allows to disable the 
default ungreedy option of RegExp.Replace().

Regards,

-- 
Benoît Minisini




More information about the User mailing list