[Gambas-user] RegExp.Replace() question

Tobias Boege taboege at ...626...
Sat Apr 29 16:30:22 CEST 2017


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

-- 
"There's an old saying: Don't change anything... ever!" -- Mr. Monk




More information about the User mailing list