Tapatalk

How to use non-greedy correctly?

How to use non-greedy correctly?

8

PostMar 10, 2023#1

How do I make the search pattern <SP (.+:)> non-greedy so that it picks up in

Code: Select all

<SP Hy:> Ek kom na my tuin,<CI><PI2> my suster, my bruid.<CI><PI> Ek pluk my mirre met my speserye,<CI><PI> ek eet my heuningkoek<CI><PI2> met my heuning,<CI><PI> ek drink my wyn met my melk.<SP Koor:><CI><PI> Eet, vriende!<CI><PI> Drink, en word van liefde dronk!<CM>
just <SP Hy:> and on next search just <SP Koor:> and not everything from <SP Hy:> to <SP Koor:>?

I have tried everything I could think of, to no avail.

6,824625
Grand MasterGrand Master
6,824625

PostMar 10, 2023#2

There can be used as search expression either <SP (.+?:)> or <SP ([^:]+:)> which both stop matching characters on next colon and the first one also on newline characters like carriage return and line-feed. ? after a multiplier like * or + means non-greedy, see Repetition with Star and Plus and best also Possessive Quantifiers.

19276
MasterMaster
19276

PostMar 11, 2023#3

Just be careful because ? itself can be used as a quantifier as well. Personally, I suggest to think of quantifiers in this way:

greedy

* any to 0
+ any to 1
? 1 to 0
{n,m}        m to n

non-greedy

*? 0 to any
+? 1 to any
?? 0 to 1
{n,m}?      n to m

I believe It helps to better understand how the regexp engine works.

BR, Fleggy