Jun-01-2020, 06:25 AM
import re RE_QUESTION_MARK = 'yd?' RE_ASTERISK = 'yd*' test_str1 = 'ydddddd' print(f'{test_str1} match result : {re.search(RE_ASTERISK, test_str1)}') print(f'{test_str1} match result : {re.search(RE_QUESTION_MARK, test_str1)}')
Output:ydddddd match result : <re.Match object; span=(0, 7), match='ydddddd'>
ydddddd match result : <re.Match object; span=(0, 2), match='yd'>
according to the match results, '*' is greedy, acceptable. '?' only match 0 or 1 repetitions of the preceding RE, it's non-greedy. How can we call it greedy?