-1

I understood that selects according normal quantify and returns the minimum value, but I don't understand why it's define the selection of next regexp atom to not be greedy. Does anyone know if there is a primitive expression that represents a reluctant quantifier(if it exists)? if I see the expression I can understand, but I searched and found only references to the joint use of ?= and ?! but I don't know if this is the case because I couldn't use it.

How to Works(I guess):

console.log("greedy regexp example(Expected  \"'blablabla' is a 'bla'\")");
console.log(("'blablabla' is a 'bla' so...").match(/'.*'/));
console.log("non-greedy regexp example(Expected \"''\" but returns \"'blablabla'\")");
console.log(("'blablabla' is a 'bla' so...").match(/'.*?'/));
  • Could you please clarify the first sentence of the post? Currently, it’s grammatically incorrect, so it’s hard to understand what you expect and why. What do you think is greedy and non-greedy in `/'.*?'/`? I don’t understand why you expect `''` for the second regex. The non-greedy match is behaving correctly there. What exactly do you expect and why? If you only expect to match the delimiters, then what you’re expecting is not a non-greedy match; what is the point of matching the already known delimiters with regex? – Sebastian Simon Feb 25 '21 at 03:50
  • I expected ```''``` because in a example ```("abla").match(/a.*?/);``` returns ```"a"``` as the logic about the algorithm is mentioned, however if I put ```("abla").match(/a.*?a/);``` its returns the whole value – Rafael Lucas Feb 25 '21 at 03:55
  • `("abla").match(/a.*?/)` returns `[ "a" ]`, because `.*?` means _“match any character (`.`), zero or more times (`*`), as few times as possible (`?`)”_. _“as few times as possible”_ is different depending on what else needs to be matched around this subexpression. – Sebastian Simon Feb 25 '21 at 03:58

1 Answers1

2

* makes expressions greedy

? makes expressions lazy

In this case, match(/'.*?'/) is read as "single quote followed by any character(.) zero or more times(*), but only before the next (?) single quote"

Most regex cheat sheets will describe these kinds of expressions. Personally, I like https://www.rexegg.com/regex-quickstart.html (states * is greedy)

What do 'lazy' and 'greedy' mean in the context of regular expressions? states * as greedy

https://docs.oracle.com/javase/tutorial/essential/regex/quant.html states * as greedy.

Karl Galvez
  • 843
  • 6
  • 15
  • _“`*` makes expressions greedy”_ — No, `*` is a quantifier, which, when used after a token, makes the regex match that token zero or more times. It has nothing to do with greedyness. The absence of the qualifier `?` (after the quantifier) is what makes a subexpression greedy. – Sebastian Simon Feb 25 '21 at 03:46
  • 1
    If the `?` is needed to make '*' non-greedy, how is `*` not greedy? Do you have any resources stating this? Everything I've looked at classifies `*` as greedy. – Karl Galvez Feb 25 '21 at 04:02
  • It’s not `*` that is greedy. Everything is greedy by default, unless the `?` qualifier is present. – Sebastian Simon Feb 25 '21 at 04:03
  • 1
    `.` is not greedy, nor is the single quote in the example. – Karl Galvez Feb 25 '21 at 04:06
  • Yes, it is. `.` matches only a single character. Once the match can be done, the match is done, and after that, there’s nothing else this could possibly match. This _is_ greedy. If it wasn’t greedy, `.` would almost always match nothing, e.g. `"abc".match(/./)` would be `null` or `[ "" ]`, but it’s not. Non-greedy single `.` would look like `/.??/`, where the first `?` means `{0, 1}` (at least 0, at most 1), and the second `?` means _“take as few characters as possible”_ (0 in this case). – Sebastian Simon Feb 25 '21 at 04:07
  • Actually, talking about non-greedy vs. greedy for single tokens doesn’t really make sense, because they are always going to be quantified as _at least 1, at most 1_ by default. Only a quantifier like `+` or `*` changes the lower and upper bounds, and only then talking about greediness makes sense. The articles talk about greedy behavior for `.+`, `.*`, and so on, but it’s not the quantifier that makes them greedy; it’s the absence of a _qualifier_. Articles that call it “greedy quantifiers” are wrong, because this phrase doesn’t make sense. – Sebastian Simon Feb 25 '21 at 05:10