greedy, non-greedy
import re
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')
match_non_greedy = re.match(r'<.*?>', s1)
if match_non_greedy:
print('non-greedy reslut: {}'.format(match_non_greedy.group()))
else:
print('Not match')
html_tags = re.findall(r'<.*?>', s1)
for idx, html_tag in enumerate(html_tags):
print("{}:{}".format(idx, html_tag))
西元日期
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')
參考