# re 正則表達式 ****** 學符號 單字符 範圍 重複 位置 分組 精準 寫不出來百度去
# http://www.cnblogs.com/Eva-J/articles/7228075.html#_label10
'''
re模塊
主要正則表達式相關
什麼是正則表達式 一堆帶有特殊意義的符號組成式子
它的做用 處理(匹配 查找 替換 )字符串
1.
在爬蟲中大量使用 其實有框架幫你封裝了這些複雜的正則
2.
在網站和手機app的註冊功能中大量使用 例如判斷你的郵箱地址是否正確
'''
'''
import re
# ===============================單個字符匹配=========print(re.findall("\n","1\n")) # 匹配換行符print(re.findall("\t","1asasas121 \t")) # 匹配製表符# ========================範圍匹配===========print(re.findall("\w","1aA_*")) # 匹配數字字母下劃線print(re.findall("\W","1aA_*,")) # 匹配非數字字母下劃線print(re.findall("\s"," \n\r\t\f")) # 匹配任意空白字符print(re.findall("\S"," \n\r\t\f")) # 匹配任意非空白字符print(re.findall("\d","123abc1*")) # 匹配任意非空白字符print(re.findall("\D","123abc1*")) # 匹配任意非空白字符# print(re.findall("[abc]","AaBbCc")) # 匹配 a b c都行# print(re.findall("[^abc]","AaBbCc")) # 除了 a b c都行# print(re.findall("[0-9]","AaBbCc12349")) # 除了 a b c都行print(re.findall("[a-z]","AaBbCc12349")) # a-z 英文字母print(re.findall("[A-z]","AaBbC:c😀2349[]")) # A-z 匹配原理 是按照ascII碼錶# =========================匹配位置======print(re.findall("\A\d","123abc1*")) # 從字符串的開始處匹配print(re.findall("\d\Z","123abc1*9\n")) # 從字符串的結束處匹配 注意把\Z寫在表達式的右邊print(re.findall("\d$","123abc1*9")) # 從字符串的結束處匹配 若是末尾有換行 換行不會參與匹配print(re.findall("^\d","s1asasas121 \t")) # 從字符開始匹配數字# [] 範圍匹配 中間 用-來鏈接# re.findall("[a-zA-Z0-9]","a ab abc abcd a123c")# 若是要匹配 符號- 要寫表達式的左邊或右邊# print(re.findall("[-ab]","a ab abc abcd a123c a--"))# 重複匹配 表達式的匹配次數# * 表示 任意次數 因此0次也知足print(re.findall("[a-zA-Z]*","a ab abc abcdssdsjad a123c"))# [a-zA-Z]*# + 一次或屢次print(re.findall("[a-zA-Z]+","a ab abc abcdssdsjad a123c"))# [a-zA-Z]+# ? 0次或1次print(re.findall("[a-zA-Z]?","a ab abc abcdssdsjad a123c"))# {1,2} 自定義匹配次數 {1,} 1到無窮 {,1} 0到1次print(re.findall("[a-zA-Z]{1,2}","a ab abc abcdsdssjad a123c"))# + * 貪婪匹配 表達式匹配的狀況下 儘量的多拿 (一直匹配 直到不知足爲止)# print(re.findall("\w*","jjsahdjshdjssadsa dssddsads"))# print(re.findall("\w+","jjsahdjshdjssadsa dssddsads"))# 非貪婪匹配 在表達式的後面加上?# print(re.findall("\w?","jjsahdjshdjssadsa dssddsads")) # 非貪婪匹配#text = """<img src="https://ss1.baidu.com/6ONXsjip0@#4$QIZ8tyhnq/it/u=2972240716,3143951157&fm=55&app=22&f=JPEG?w=121&h=81&s=0FB86D855C624C9C0AB1F57203008031" class="c-img c-img6">"""# 演示貪婪匹配print(re.findall('src="(http.*?)"',text))# 非貪婪匹配 在表達式的後面加上?print(re.findall('src="http.*"',text))# print(re.findall("[a-zA-Z]*","a|ab|abc|abcd|a123c"))# 分組 加上分組 不會改變原來的規則 僅僅是將括號中的內容單獨拿出來了print(re.findall("([a-zA-Z]+)_dsb","aigen_dsb cxx_dsb alex_dsb zxx_xsb _dsb"))# re模塊中經常使用的函數# match 從字符串開始處匹配 只找一個print(re.match("\w*","abc").group(0)) # 獲取匹配成功的內容# group 用來獲取某個分組的內容 默認獲取第0組 就是整個表達式自己print(re.match("([a-zA-Z]+)(_dsb)","aigen_dsb cxx_dsb alex_dsb zxx_xsb _dsb").group(2))print(re.match("\w*","abc").span()) # 獲取匹配成功的內容的索引print(re.search("\w*","abc").group())# 從全文範圍取一個print(re.search("([a-zA-Z]+)(_dsb)","xxx aigen_dsb cxx_dsb alex_dsb zxx_xsb _dsb"))# 從開始的位置開始匹配# print(re.match("([a-zA-Z]+)(_dsb)","xxx aigen_dsb cxx_dsb alex_dsb zxx_xsb _dsb").group())# 將正則表達式 編譯成一個對象 日後能夠不用在寫表達式 直接開始匹配# print(re.compile("\w*").findall("abcd"))# print(re.split("\|_*\|","python|____|js|____|java"))# 替換print(re.sub("python","PYTHON","js|python|java"))# 用正則表達式來交換位置text = "java|C++|js|C|python"# text1 = "java|C++|js|C|python"# 將整個內容分爲三塊 java |C++xxxxxx| pythonpartten = "(.+?)(\|.+\|)(.+)"".+?ahgshags"# ?:用於取消分組 就和沒寫括號同樣# partten = "(?:.+?)(\|.+\|)(.+)"# print(re.search(partten,text).group(0))print(re.sub(partten,r"\2\3\1",text))# 當要匹配的內容包含\時text = "a\p""\p"print(text)print(re.findall(r"a\\p",text))'''# subprocess *** 子進程 python和另外一個進程交換數據 Popen(命令,輸出管道,輸入摜蛋,錯誤管道)'''import subprocess# 就用來執行系統命令import oscmd = r'dir H:\PYTHON-DUJUN\day23 | findstr "py"'res = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)# # 從管道中讀取數據 管道就是 兩個進程通信的媒介# print(type(res.stdout.read().decode("GBK")))print(res.stdout.read().decode("GBK"))print(res.stderr.read().decode("GBK"))dir = r'dir D:\上海python全棧4期\day23'find = 'findstr "py"'"""stdout 輸出管道stdin 輸入管道stderr 錯誤管道"""# res1 = subprocess.Popen(dir,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)# res2 = subprocess.Popen(find,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE,stdin=res1.stdout)# 從管道中讀取數據 管道就是 兩個進程通信的媒介# print(type(res.stdout.read().decode("GBK")))# print(res1.stdout.read().decode("GBK"))# print(res2.stderr.read().decode("GBK"),"33333")# 簡單總結 subprocess 主要用於執行系統指令 (啓動子進程) 與os.system的不一樣在於# subprocess 能夠與這個子進程進行數據交換'''