目錄 |
-
選做題:你的正則表達式也可以支持其他高級域名,如.edu、.net 等(例如, http://www.foothill.edu)
-
匹配所有能夠表示 Python 整數的字符串集
-
匹配所有能夠表示 Python 長整數的字符串集
-
匹配所有能夠表示 Python 浮點數的字符串集
-
匹配所有能夠表示 Python 複數的字符串集
內容 |
1. 識別後續的字符串: “bat”、“ bit”、“ but”、“ hat”、“ hit”或者“hut”
import re
# 1-1 識別後續的字符串: “bat”、“ bit”、“ but”、“ hat”、“ hit”或者“hut”。
def test_one(test_string):
bt = 'bat|bit|but|hat|hit|hut' #匹配多個字符串
#bt = '[bh][aiu]t' #創建字符集
m = re.match(bt,test_string)
if m is not None:
print('匹配成功%s'%(m.group()))
else:
print('匹配不成功')
if __name__ == '__main__':
test_string_1 = 'bat'
test_string_2 = 'hct'
test_one(test_string_1)
test_one(test_string_2)
運行結果
PS C:\Users\ass> & D:/anaconda3/python.exe "d:/新建文件夾 (3)/1-1.py"
匹配成功bat
匹配不成功
2. 匹配由單個空格分隔的任意單詞對,也就是姓和名
import re
def test_two(test_string):
# reg = '[A-Za-z]+ [A-Za-z]+'
reg = '[A-Za-z]+\s[A-Za-z]+'
name = re.match(reg,test_string)
if name is not None:
print(name.group())
if __name__ == '__main__':
test_string_1 = 'Isaac Newton'
test_string_2 = 'William Shakespeare'
test_string_3 = 'Sonerye'
test_two(test_string_1)
test_two(test_string_2)
test_two(test_string_3)
運行結果
PS C:\Users\ass> & D:/anaconda3/python.exe "d:/新建文件夾 (3)/1-2.py"
Isaac Newton
William Shakespeare
補充:\s 匹配空格
3. 匹配由單個逗號和單個空白符分隔的任何單詞和單個字母,如姓氏的首字母
import re
def test_three(test_string):
name = re.findall('\w+',test_string,re.I)
print(name)
if __name__ == '__main__':
test_string_1 = 'Isaac Newton'
test_string_2 = 'William Shakespeare'
test_string_3 = 'Sonerye,A,B,c'
test_three(test_string_1)
test_three(test_string_2)
test_three(test_string_3)
運行結果
PS C:\Users\ass> & D:/anaconda3/python.exe "d:/新建文件夾 (3)/1-3.py"
['Isaac', 'Newton']
['William', 'Shakespeare']
['Sonerye', 'A', 'B', 'c']
4. 匹配所有有效 Python 標識符的集合
import re
def test_four(test_string):
name = re.match('^[A-Za-z_]\w+$',test_string)
if name is not None:
print(name.group())
if __name__ == '__main__':
test_string_1 = '$abc'
test_string_2 = '_aeuhu23'
test_string_3 = 'sjnkxn_ wefe'
test_four(test_string_1)
test_four(test_string_2)
test_four(test_string_3)
運行結果
PS C:\Users\ass> & D:/anaconda3/python.exe "d:/新建文件夾 (3)/1-4.py"
_aeuhu23
5. 根據讀者當地的格式,匹配街道地址(使你的正則表達式足夠通用,來匹配任意數 量的街道單詞,包括類型名稱)。例如,美國街道地址使用如下格式:1180 Bordeaux Drive。使你的正則表達式足夠靈活,以支持多單詞的街道名稱,如 3120 De la Cruz Boulevard
import re
def test_five(test_string):
name = re.match('^[\d]+ [\w+ ]+',test_string)
if name is not None:
print(name.group())
if __name__ == '__main__':
test_string_1 = '3120 De la Cruz Boulevard'
test_string_2 = '1180 Bordeaux Drive'
test_string_3 = 'abc def cdb'
test_five(test_string_1)
test_five(test_string_2)
test_five(test_string_3)
運行結果
PS C:\Users\ass> & D:/anaconda3/python.exe "d:/新建文件夾 (3)/1-5.py"
3120 De la Cruz Boulevard
1180 Bordeaux Drive
6. 匹配以“www”起始且以“.com”結尾的簡單Web 域名;例如,www://www. yahoo.com/。
import re
def test_six(test_string):
name = re.match('www.+\.com',test_string)
if name is not None:
print(name.group())
if __name__ == '__main__':
test_string_1 = 'www://www. yahoo.com/'
test_string_2 = 'www.hekdhk.com'
test_string_3 = 'www.dsskkncom'
test_six(test_string_1)
test_six(test_string_2)
test_six(test_string_3)
運行結果
PS C:\Users\ass> & D:/anaconda3/python.exe "d:/新建文件夾 (3)/1-6.py"
www://www. yahoo.com
www.hekdhk.com
7. 選做題:你的正則表達式也可以支持其他高級域名,如.edu、.net 等(例如, http://www.foothill.edu)
關注我,帶你一起寫bug
warning :未經授權,不得轉載
有問題的小夥伴請在下方留言,喜歡就點個贊吧