Difference between .* and .*? in regex pattern

428 阅读1分钟

We all know that we can use .* to match any characters with zero or more times. But in some cases it is useful to match as shorter as possible that satisfies our need. Here comes the question mark, it can cater to this case easily.
For example we have a string as "aabab", if regex pattern is written as a.*b, then aabab is what we get. but if regex pattern is a.*?b, then we can get aab and ab as two results. The question mark in regex pattern denotes lazy mode, which means it will match the shortest string to satisfy the pattern.