Capturing zero or more characters in regex while being non-greedy

Solution 1:

As mentioned in my other answer, the .*? version is lazy. That means it will make the parenthesized part apply to as little as possible for the regex to match. The version with the question mark outside the parentheses is greedy: it will apply to as many characters as possible.

Both expressions will have the same effect if you only look at getting a match versus not. The two wildcard runs will get a match if possible, but one minimizes the captured amount while the other maximizes it. They differ, however, in what they capture.

Let's see an example of the capturing differences: singing. It contains the literal ng twice. In (.*?)ng, (.*?) will grab everything up until the first ng - once it sees that, it's done: it's lazy. It will capture si in this case. (.*)?ng will try to capture as much as possible - it's greedy - leaving only the final ng out. It captures singi here.

Unless there are multiple instances of the part outside the wildcard run in the input string, you won't see a difference.