每天CookBook之Python-024

  • 使用正则表达式匹配字符串
  • 使用findall获取子字符串
  • 使用正则表达式获取子字符串
text = 'yeah, but no, but yeah, but no, but yeah'
text1 = '11/27/2012'
text2 = 'Nov 27, 2012'

import re

if re.match(r'\d+/\d+/\d+', text1):
    print('yes')
else:
    print('no')

if re.match(r'\d+/\d+/\d+', text2):
    print('yes')
else:
    print('no')

datepat = re.compile(r'\d+/\d+/\d+')

if datepat.match(text1):
    print('yes')
else:
    print('no')

if datepat.match(text2):
    print('yes')
else:
    print('no')

text = 'Today is 11/27/2012. PyCon starts 3/13/2013.'
print(datepat.findall(text))

datepat = re.compile(r'(\d+)/(\d+)/(\d+)')
m = datepat.match('11/27/2012')

print(m.group(0))
print(m.group(1))
print(m.group(2))
print(m.group(3))
print(m.groups())

month, day, year = m.groups()

print(datepat.findall(text))
for month, day, year in datepat.findall(text):
    print('%s-%s-%s' % (year, month, day))
yes
no
yes
no
['11/27/2012', '3/13/2013']
11/27/2012
11
27
2012
('11', '27', '2012')
[('11', '27', '2012'), ('3', '13', '2013')]
2012-11-27
2013-3-13
posted @ 2016-07-12 22:55  4Thing  阅读(69)  评论(0编辑  收藏  举报