[Gambas-user] RegExp.Replace() question (was: sliderbox bug: Still respond to mouse wheel when its disabled)

Tobias Boege taboege at ...626...
Sat Apr 29 13:48:09 CEST 2017


On Fri, 28 Apr 2017, Fernando Cabral wrote:
> Hi, gambas is new to me, but regex is not so new. But I am baffled with the
> following result:
> 
> str = "A#BB##CCC###"
> print RegExp.Replace(str, "[#]+", ";")
>     A;BB;;CCC;;;
> str = "A#BB##CCC###"
> print RegExp.Replace(str, "[#][#]*", ";")
>     A;BB;;CCC;;;
> str = "A#BB##CCC###"
> print RegExp.Replace(str, "##**", ";")
>     A;BB;;CCC;;;
> 
> In my opinion, in every example above the result should be:
> A;BB;CC
> Nevertheless, gambas always displays A;BB;CCC;;;.
> 
> Am I missing something,, or does gambas has a bug in this point?
> 

We had this topic a few days ago [1]. When you use RegExp.Replace(),
the greediness of all quantifiers is inverted. So when you use

  "[#]+"

under RegExp.Replace(), this has the same meaning as the expression
(in PCRE syntax)

  "[#]+?"

matching lazily (or ungreedily) against the string, which explains
the result you get. If you want greedy behaviour (what you expected),
then you have to use ungreedy quantifiers:

  Print RegExp.Replace("A#BB##CCC###", "[#]+?", ";")
  Print RegExp.Replace("A#BB##CCC###", "[#][#]*?", ";")
  Print RegExp.Replace("A#BB##CCC###", "##*?", ";")
  ---
  A;BB;CCC;
  A;BB;CCC;
  A;BB;CCC;

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.

Regards,
Tobi

[1] https://sourceforge.net/p/gambas/mailman/message/35802891/

PS: When you want to talk about a new topic, make a new thread and don't
    compose your email by replying to an existing message from the mailing
    list. Currently our conversation about RegExp.Replace() takes place
    in a thread about a specific SliderBox bug, which makes it harder for
    people in the future who are searching for this discussion.

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




More information about the User mailing list