Python Forum
RE-greedy or non greedy about '?'
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
RE-greedy or non greedy about '?'
#3
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?
Reply


Messages In This Thread
RE-greedy or non greedy about '?' - by frank0903 - Jun-01-2020, 04:17 AM
RE: RE-greedy or non greedy about '?' - by buran - Jun-01-2020, 04:38 AM
RE: RE-greedy or non greedy about '?' - by frank0903 - Jun-01-2020, 06:25 AM
RE: RE-greedy or non greedy about '?' - by buran - Jun-01-2020, 08:17 AM
RE: RE-greedy or non greedy about '?' - by buran - Jun-01-2020, 10:03 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020