練習1:
# -*- coding: utf-8 -*-
"""
有一個文件,文件名為output_1981.10.23.txt
下面使用Python:讀取文件名中的日期時間信息,並找出這一天是周幾
將文件改名為output_YYYY-MM-DD-W.txt
(YYYY:四位的年,MM:兩位的月份,DD:兩位的日,W:一位的周幾,並假設周一為一周第一天)
"""
import re
file_name = 'output_1981.10.21.txt'
f_pattern = re.compile("\d{4}\.\d{2}\.\d{2}")
# type: str
f_date = re.search(f_pattern, file_name).group()
print "date: {}".format(f_date) # 1981.10.21
# week message
if int(f_date.split('.')[2]) % 7 == 0:
week = 7
else:
week = int(f_date.split('.')[2]) % 7
print "week: {}".format(week)
# new date
n_year = f_date.split('.')[0][2:]
n_month = f_date.split('.')[1]
n_day = f_date.split('.')[2]
練習2:
import re
doc = "Outlook.com Postmaster provides abc@hotmail.com for the administrators of systems sending"
pattern = re.compile("(\w+@?\w+)\.com", flags=re.IGNORECASE)
print re.search(pattern, doc).group(0)
print re.findall(pattern, doc)
練習3:
# -*- coding: utf-8 -*-
"""
匹配email地址
"""
import re
doc = "abc_cde@hotmail.com ha.haha@123.com abc.cde@21vianet.com abc@hotmail.com.cn for the abc.cde_efg@hotmail.com.cn"
pattern = re.compile("\w+_?\.?\w+@+\w+\.+\w+\.?\w*", flags=re.IGNORECASE)
print re.findall(pattern, doc)
Email地址的匹配邏輯
