標籤:資料 short 匹配 com drive 網址 ctime txt 名稱
1. 識別後續的字串:“bat”,“bit”,“but”,“hat”,“hit”或者“hut”。
[bh][aiu]t
2. 匹配由單個空格分割的任意單詞對,也就是姓和名。
[A-Za-z]+\s[A-Za-z]+
3. 匹配由單個逗號和單個空白符分隔的任何單詞和單個字母,如姓氏的首字母
[A-Za-z]+,\s[A-Za-z]
4. 匹配所有的有效Python標識符集合
[A-Za-z_]\w+ # 匹配任意以字母和底線開頭,標識符可以包括字母,底線和數字。
5. 根據美國接到地址格式,匹配街道地址。美國街道地址使用如下格式:1180 Bordeaux Drive。使你的Regex足夠靈活,以支援多單詞的街道名稱,如3120 De la Cruz Boulevard
\d+[a-zA-Z\s]+
6. 匹配以“www”起始且以“.com”結尾的簡單Web網域名稱:例如,www://www.yahoo.com。
^www[^\s]+[com|edu|net]$
7. 匹配所有能夠表示Python整數的字串集
[-+]?\d+ # 有正負符號或無
8. 匹配所有能夠表示Python長整數的字串集
[-+]?\d+[lL] # 長整型,整數後面跟大寫或小寫L
9. 匹配所有能夠表示Python浮點數的字串集
[-+]?\d+\.\d*
10. 表示所有能夠表示Python複數的字元集
-?\d+\+[\+-]\d+j
11. 匹配所有能夠表示有效電子郵件的集合
[^\s][email protected](\w+\.?)+
12. 匹配所有能夠表示有效網址的集合(URL)
(http://([A-Za-z0-9\-]+\.?)+)
13. 建立一個能從字串中提取實際類型名稱的Regex。函數將對於<type ‘int‘>字串返回int
<type\s\‘(\w+)\‘>
14. 處理日期,常見一個Regex來表示標準日曆中剩餘三個月的數字
1[012]
15. 處理信用卡號碼
(\d{4}-\d{6}-\d{5}|\d{4}-\d{4}-\d{4}-\d{4})
16. 為gendata.py更新代碼,是資料直接輸出到redata.txt而不是螢幕
# !/usr/bin/env python# gendata.pyfrom random import randrange, choicefrom string import ascii_lowercase as lc# from sys import intfrom time import ctimetlds = (‘com‘, ‘edu‘, ‘net‘, ‘org‘, ‘gov‘)with open(‘path\redata.txt‘, ‘w‘) as fp: for i in range(randrange(5,11)): dtint = randrange(2**32) # pick date dtstr = ctime(dtint) # date string llen = randrange(4, 8) # login is shorter login = ‘‘.join(choice(lc) for j in range(llen)) dlen = randrange(llen, 13) # domain is longer dom = ‘‘.join(choice(lc) for j in range(dlen)) print(‘%s::%[email protected]%s.%s::%d-%d-%d‘ % (dtstr, login, dom, choice(tlds), dtint, llen, dlen)) str = ‘%s::%[email protected]%s.%s::%d-%d-%d‘ % (dtstr, login, dom, choice(tlds), dtint, llen, dlen) fp.write(str+‘\n‘)
17. 判斷redata.txt中一周的每一天出現的次數
import reweek = {"Mon": 0, "Tue": 0, "Wed": 0, "Thu": 0, "Fri": 0, "Sat": 0, "Sun": 0}with open(‘path\redata.txt‘, ‘r‘) as fp: data = fp.readlines() for line in data: day = re.match(‘^\w{3}‘, line) key = day.group() week[key] = week[key] + 1print(week)
18. 提取
# 判斷資料是否損壞Sat Jun 22 09:14:03 1985::import rewith open(‘path\redata.txt‘, ‘r‘) as fp: data = fp.readlines() for line in data: time = re.match(‘^\w{3}\s\w{3}\s{1,2}\d{1,2}\s\d{2}:\d{2}:\d{2}\s\d{4}‘, line) if time is not None: print(‘未損毀‘) else: print(‘存在資料損毀‘)
19-25. 建立Regex
# 19-27練習import retimes = []emails = []months = []years = []clocks = []lnames = []dnames = []with open(‘path\redata.txt‘, ‘r‘) as fp: data = fp.readlines() for line in data: time = re.match(‘(\w{3}\s(\w{3})\s{1,2}\d{1,2}\s(\d{2}:\d{2}:\d{2})\s(\d{4}))::((\w+)@((\w+\.?)+))::‘, line) # 提取時間戳記 if time is not None: times.append(time.group(1)) # 提取完整時間戳記 months.append(time.group(2)) # 提取月份 years.append(time.group(4)) # 提取年份 clocks.append(time.group(3)) # 提取時間 emails.append(time.group(5)) # 提取郵件 lnames.append(time.group(6)) # 提取登入名稱 dnames.append(time.group(7)) # 提取網域名稱print(‘完整時間:‘, times)print(‘月份:‘, months)print(‘年份:‘, years)print(‘時間點:‘, clocks)print(‘郵件地址:‘, emails)print(‘登入名稱:‘, lnames)print(‘網域名稱:‘, dnames)
結果:
26. 使用自己的電子郵件地址替換沒一行資料中的電子郵件地址
import rewith open(‘path\redata2.txt‘, ‘r‘) as fp: data = fp.readlines() for line in data: print(re.sub(‘\[email protected](\w+.?)+‘, ‘[email protected]‘, line).rstrip())
結果:
27. 從時間戳記中提取月、日、年,然後以“月、日、年”的格式。
import ref = open(‘redata.txt‘,‘r‘)with open(‘path\redata2.txt‘, ‘r‘) as fp: data = fp.readlines() for line in data: m = re.match(‘^\w{3}\s(\w{3})\s{1,2}(\d{1,2})\s\d{2}:\d{2}:\d{2}\s(\d{4})‘, line) print(‘%s,%s,%s‘ % (m.group(1), m.group(2), m.group(3)))
結果:
《python核心編程》——Regex學習筆記(課後練習)