iT邦幫忙

2018 iT 邦幫忙鐵人賽
0

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

參考


上一篇
Day24-re-Syntax
下一篇
Day26-datetime-概觀
系列文
if len(learning.python) == 30:31

尚未有邦友留言

立即登入留言