greedy, non-greedy
import re
# greedy
s1 = '<div class="qa-list__tags">sbc</div>'
match_greedy = re.match(r'<.*>', s1)
if match_greedy:
print('greedy reslut: {}'.format(match_greedy.group()))
else:
print('Not match')
# greedy reslut: <div class="qa-list__tags">sbc</div>
# non-greedy
match_non_greedy = re.match(r'<.*?>', s1)
if match_non_greedy:
print('non-greedy reslut: {}'.format(match_non_greedy.group()))
else:
print('Not match')
# non-greedy reslut: <div class="qa-list__tags">
html_tags = re.findall(r'<.*?>', s1)
for idx, html_tag in enumerate(html_tags):
print("{}:{}".format(idx, html_tag))
# 0:<div class="qa-list__tags">
# 1:</div>
西元日期
import re
s = 'Ray\'s birthday is 2018-1-3, and John\'s birthday is 2018-01-03'
dates = re.findall(r'\d{4}-\d{1,2}-\d{1,2}', s)
if dates:
for idx, date in enumerate(dates):
print("{}:{}".format(idx, date))
else:
print('Not match')
# 0:2018-1-3
# 1:2018-01-03
參考