re
.findall('alvin','yuanalesxalexwupeiqi')
['alvin']
import re
ret=re.findall('a..in','helloalvin')
print(ret)#['alvin']
ret=re.findall('^a...n','alvinhelloawwwn')
print(ret)#['alvin']
ret=re.findall('a...n$','alvinhelloawwwn')
print(ret)#['awwwn']
ret=re.findall('a...n$','alvinhelloawwwn')
print(ret)#['awwwn']
ret=re.findall('abc*','abcccc')#貪婪匹配[0,+oo]
print(ret)#['abcccc']
ret=re.findall('abc+','abccc')#[1,+oo]
print(ret)#['abccc']
ret=re.findall('abc?','abccc')#[0,1]
print(ret)#['abc']
ret=re.findall('abc','abccc')
print(ret)#['abccc'] 貪婪匹配
ret=re.findall('abc*?','abcccccc')
print(ret)#['ab']
#--------------------------------------------字符集
ret=re.findall('a[bc]d','acd')
print(ret)#['acd']
ret=re.findall('[a-z]','acd')
print(ret)#['a', 'c', 'd']
ret=re.findall('[.*+]','a.cd+')
print(ret)#['.', '+']
#在字符集裡有功能的符號: - ^ \
ret=re.findall('[1-9]','45dha3')
print(ret)#['4', '5', '3']
ret=re.findall('[^ab]','45bdha3')
print(ret)#['4', '5', 'd', 'h', '3']
ret=re.findall('[\d]','45bdha3')
print(ret)#['4', '5', '3']
\d 匹配任何十進位制數;它相當於類 [0
-9]。
\d 匹配任何非數字字元;它相當於類 [^0
-9]。
\s 匹配任何空白字元;它相當於類 [ \t\n\r\f\v]。
\s 匹配任何非空白字元;它相當於類 [^ \t\n\r\f\v]。
\w 匹配任何字母數字字元;它相當於類 [a-za-z0-9_]。
\w 匹配任何非字母數字字元;它相當於類 [^a-za-z0-9_]
\b 匹配乙個特殊字元邊界,比如空格 ,&,#等
ret=re.findall('i\b','i am list')
print(ret)#
ret=re.findall(r'i\b','i am list')
print(ret)#['i']
#-----------------------------eg1:
import re
ret=re.findall('c\l','abc\le')
print(ret)#
ret=re.findall('c\\l','abc\le')
print(ret)#
ret=re.findall('c\\\\l','abc\le')
print(ret)#['c\\l']
ret=re.findall(r'c\\l','abc\le')
print(ret)#['c\\l']
#-----------------------------eg2:
#之所以選擇\b是因為\b在ascii表中是有意義的
m = re.findall('\bblow', 'blow')
print(m)
m = re.findall(r'\bblow', 'blow')
print(m)
m = re.findall(r'(ad)+', 'add')
print(m)
ret=re.search('(?p\d)/(?p\w)','23/com')
print(ret.group())#23/com
print(ret.group('id'))#23
ret=re.search('(ab)|\d','rabhdg8sd')
print(ret.group())#ab
import re
#1re.findall('a','alvin yuan') #返回所有滿足匹配條件的結果,放在列表裡
#2re.search('a','alvin yuan').group() #函式會在字串內查詢模式匹配,只到找到第乙個匹配然後返回乙個包含匹配資訊的物件,該物件可以
通過呼叫group()方法得到匹配的字串,如果字串沒有匹配,則返回none。
#3re.match('a','abc').group() #同search,不過盡在字串開始處進行匹配
#4ret=re.split('[ab]','abcd') #先按'a'分割得到''和'bcd',在對''和'bcd'分別按'b'分割
print(ret)#['oldboy'] 這是因為findall會優先把匹配結果組裡內容返回,如果想要匹配結果,取消許可權即可
#匹配出所有的整數
import re
#ret=re.findall(r"\d+]","1-2*(60+(-40.35/5)-(-4*3))")
ret=re.findall(r"-?\d+\.\d*|(-?\d+)","1-2*(60+(-40.35/5)-(-4*3))")
ret.remove("")
print(ret)
python學習筆記 四 模組
模組實現函式重用,import匯入模組。import sys for i in sys.argv print i 執行結果 c users liyz b desktop work func.py 其中,sys模組包含了與python直譯器和環境有關的函式。sys.argv表示包含了命令列引數的字串列...
python 四 常用模組學習
pillow模組 用於影象處理。requests模組 處理網頁鏈結請求響應。res requests.get url params params可選。res requests.post url data json files 分別是引數傳遞 json資料傳遞,和檔案上傳。可選。res.text 獲得...
Python常用模組 四
正規表示式時電腦科學的乙個概念,正規表示式通常被用來檢索,替換那些符合某個模式的文字,大多數程式語言都支援利用正規表示式進行字串操作.正則就是用一些具有特殊含義的符號組合到一起來描述字元或者字串的方法,或者說正則就是用來描述一類事物的規則.它內嵌在python中,並通過re模組來實現,正規表示式模式...