Python regular expression matching priority

In Python regular expressions, the default is to match the most characters, which is greedy matching, such as:

 String: abbbab Regular expression: a. * B The result is abbbab

If you want to match the fewest characters, just add * after the *? , Such as a. *? B, this means: repeat any number of times, but on the premise of successful matching, match as few characters as possible, this is lazy matching


a. *? b matches the fewest characters. If there is such a string: aabab, it will match aab and ab because there are higher priority matches than greed and laziness: the first match has the highest priority.

Published 190 original articles · 19 praises · 200,000+ views

Guess you like

Origin blog.csdn.net/zengchenacmer/article/details/44626013