Regular expression greedy matching

has a question when learning greedy matching of regular expressions. In the following example,

 a.*?b  abaab  abaab

if it is said to match as few characters as possible between an and b, shouldn"t it match ab and ab? When a regular expression matches a string, it should match from left to right.
then the following example,

a*?b.  abacb
 ab  acb ab  b

I don"t quite understand. Please give me some advice. be deeply grateful!

Mar.10,2021

this is because the regular expression is backtracking from left to right (it may not be accurate, because whether backtracking is the indicator that distinguishes the DFA engine from the NFA engine).

so regular matching from left to right, one by one, until the shortest match is found, the result is extracted and the next match is carried out. So your first rule actually matches like this:

from left to right, the first shortest match that satisfies a.roomroomb is naturally ab , then the ab is extracted and continues to match backward, so the second matching result aab is obtained.

The second rule of

is purely because you misunderstand the meaning of * , which means that the preceding characters are repeated any number of times, so the shortest match of a repeat b is supposed to be 0 times for an and 1 for b, so it matches to the last b .

for more information on regular backtracking matching, please see https://zhuanlan.zhihu.com/p/.

.
Menu