I've been doing regex for a while but I'm not an expert on the subtleties of what particular rules do, I've always done (.*?)
for matching, but with restriction, as in I understood it would stop the first chance it got, whereas (.*)?
would continue and be more greedy.
but I have no real reason why I think that, I just think it because I read it once upon a time.
now I'd like to know, is there a difference? and if so, what is it...
(. *?) matches any character ( . ) any number of times ( * ), as few times as possible to make the regex match ( ? ). You'll get a match on any string, but you'll only capture a blank string because of the question mark.
represents any single character (usually excluding the newline character), while * is a quantifier meaning zero or more of the preceding regex atom (character or group). ? is a quantifier meaning zero or one instances of the preceding atom, or (in regex variants that support it) a modifier that sets the quantifier ...
Note that a* means zero or more occurrence of a in the string while a+ means that one or more occurrence of a in the string. That means a* denotes language L = {є , a, aa, aaa, ….}
There are also two types of regular expressions: the "Basic" regular expression, and the "extended" regular expression.
(.*?)
is a group containing a non-greedy match.
(.*)?
is an optional group containing a greedy match.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With