Topic on Extension talk:Replace Text

<strike>{n,m} quantifier (?)</strike> Lazy vs Greedy (Default)

4
MvGulik (talkcontribs)

Did not know "Replace Text" supported the {n,m} quantifier.
...
Well at least the {n} version that is.
When trying out the {n,m} format like in "{2,}" or "{2,99}" it just matched 2 cases, while ignoring the trailing rest. (?)

In case it matters.
Tested it with leading blank-lines removal like in "^\n{2,99}(.*)"

(Not tested {n,m} with additional trailing quantifiers ... don't really see the point of that)
(to get the job done I switched back to "^\n+?(.*)".)

MvGulik (talkcontribs)

Hmmm ... Might be related to the fact that quantifiers in 'Replace_Text' seem to be set to be lazy by default. (which I keep generally forgetting as I learned it the other way around)

Guess I need to do some additional reading & testing on this.

MvGulik (talkcontribs)

Yea, it is as expected.

RE: "a{4,8}" => "aaaaaabbbaaaaaacccaaaaaa" <= lazy mode.

RE: "a{4,8}?" => "aaaaaabbbaaaaaacccaaaaaa" <= greedy mode.


So far I don't know of any other RE-implementation that is doing it this way. Even the, on the main page linked, mysql page shows its greedy by default. (no mention of "greedy" or "lazy" though)

Although defaulting to RE-lazy-behavior for MW might not be a bad thing ...

(Makes testing 'Replace Text' intended RE code on sites like https://regexr.com/ a bit awkward, and error prone, though)

I do think this 'Lazy-Default vs Greedy' should be mentioned(!) on the 'Replace text' main page.

Lady G2016 (talkcontribs)

I was having the same problem until I saw your solution. I wanted to capture all of the characters in this string:

String: "{{Abc | 1498}}#hist=tab%3A2"

Enabling greedy mode fixed my problem. The % char seemed to cause additional difficulty, so I included it in my group.

RE: "\{\{Abc \| (\d*)\}\}([^ ]|\%)+" => "{{Abc | 1498}}#hist=tab%3A2" <= lazy mode

RE: "\{\{Abc \| (\d*)\}\}([^ ]|\%)+?" => "{{Abc | 1498}}#hist=tab%3A2" <= greedy mode

I agree that 'Lazy-Default vs. Greedy' should be mentioned on the 'Replace text' main page.

Reply to "<strike>{n,m} quantifier (?)</strike> Lazy vs Greedy (Default)"