姝e垯琛ㄨ揪寮忕殑浣跨敤
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了姝e垯琛ㄨ揪寮忕殑浣跨敤相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/%e6%ad%a3%e5%88%99%e8%a1%a8%e8%be%be%e5%bc%8f' title='姝e垯琛ㄨ揪寮?>姝e垯琛ㄨ揪寮?/a> class hello blog mpi article 妯″潡 span 鎸囧畾
#鍘熸枃:http://blog.csdn.net/Rozol/article/details/70052637
#coding=utf-8 # regular.py 姝e垯琛ㄨ揪寮?/span> import re # 姝e垯妯″潡 def regular(): data = "She is more than pretty. 520" # --- 姝e垯 --- reg = r"mo" # 鎸囧畾瀛楃 => span=(7, 9), match=鈥榤o鈥?/span> reg = r"." # (.)鍗曚釜瀛楃 => span=(0, 1), match=鈥楽鈥?/span> reg = r"." # ()杞箟绗?=> span=(23, 24), match=鈥?鈥?/span> reg = r"[.]" # ([])瀛楃闆嗗悎(娉ㄦ剰:閮ㄥ垎鐗规畩瀛楃澶卞幓鐗规畩鎰忎箟) => span=(23, 24), match=鈥?鈥?/span> reg = r"[love]" # []鍐呬换鎰忓瓧绗?=> span=(2, 3), match=鈥榚鈥?/span> reg = r"[i-u]" # (-)鑼冨洿 => span=(4, 5), match=鈥榠鈥?/span> reg = r"t{2}" # {}鍐呬负闀垮害(3涓?) => span=(20, 22), match=鈥榯t鈥?/span> reg = r"t{1,3}" # {M,} / {.N} / {N} => span=(12, 13), match=鈥榯鈥?/span> reg = r"(i|o|u){1}" # (())缁?=> span=(4, 5), match=鈥榠鈥?/span> reg = r"^S" # (^)寮€澶?=> span=(0, 1), match=鈥楽鈥?/span> reg = r"[^S]" # ([^])鍙栧弽(涓嶅惈H) => span=(1, 2), match=鈥榟鈥?/span> reg = r"520$" # ($)缁撳熬 => span=(25, 28), match=鈥?20鈥?/span> reg = r"et*" # (*)鍖归厤{0,}涓〃杈惧紡 => [鈥榚鈥? 鈥榚鈥? 鈥榚tt鈥榏 reg = r"et+" # (+)鍖归厤{1,}涓〃杈惧紡 => [鈥榚tt鈥榏 reg = r"et?" # (?)鍖归厤{0,1}涓〃杈惧紡 => [鈥榚鈥? 鈥榚鈥? 鈥榚t鈥榏 reg = r".+?e" # (?)闈炶椽濠ā寮?span=(0, 20), match=鈥楽he is more than pre鈥?=> span=(0, 3), match=鈥楽he鈥? reg = r"145" # ascii鏍囩殑8杩涘埗鏁?145=101=e) => span=(2, 3), match=鈥榚鈥?/span> reg = r"d" # (d)鍗曚釜鏁板瓧 => span=(25, 26), match=鈥?鈥?(鎺ㄨ崘:[0-9]) reg = r"D" # (D)闈炴暟瀛?=> span=(0, 1), match=鈥楽鈥?(鎺ㄨ崘:[^0-9]) reg = r"s" # (s)绌虹櫧瀛楃 => span=(3, 4), match=鈥?鈥?(鎺ㄨ崘:[ fv]) reg = r"S" # (S)闈炵┖鐧藉瓧绗?=> span=(0, 1), match=鈥楽鈥?(鎺ㄨ崘:[^ fv]) reg = r"w" # (w)鍗曡瘝 => span=(0, 1), match=鈥楽鈥?(鎺ㄨ崘:[a-zA-Z0-9_]) reg = r"W" # (W)闈炲崟璇?=> span=(3, 4), match=鈥?鈥?(鎺ㄨ崘:[^a-zA-Z0-9_]) reg = r"AS" # (A)寮€澶?=> span=(0, 1), match=鈥楽鈥?/span> reg = r"520" # ()缁撳熬 => span=(25, 28), match=鈥?20鈥?/span> reg = r"y" # ()鍗曡瘝杈圭晫(Hello) => span=(22, 23), match=鈥榶鈥?/span> reg = r"oB" # (B)闈炲崟璇嶈竟鐣?world) => span=(8, 9), match=鈥榦鈥?/span> reg = r"[01]dd|2[0-4]d|25[0-5]" # 鎴?|) 澶氫綅鏁?鍖归厤0 - 255 鐩存帴鐨勬暟瀛? index = re.search(reg, data) # 鏌ユ壘鍗曚釜鍖归厤椤?/span> index = re.match(r"She", data) # 鍖归厤寮€澶?=> span=(0, 3), match=鈥楽he鈥?/span> index = re.fullmatch(r".+", data) # 鍖归厤鍏ㄩ儴 => span=(0, 28), match=鈥楽he is more than pretty. 520鈥?/span> lists = re.findall(reg, data) # 鏌ユ壘鎵€鏈夊尮閰嶉」(鍒楄〃) lists = re.split(r"o", data, maxsplit=1) # 鏍规嵁姝e垯鍒嗗壊瀛楃涓?maxsplit鍒嗗壊娆℃暟) => [鈥楽he is m鈥? 鈥榬e than pretty. 520鈥榏 strs = re.sub(r".", r"!", data, count=1) # 鏇挎崲(count:鏇挎崲娆℃暟)(鍖归厤鏇挎崲,鏈尮閰嶅師鏍? => She is more than pretty! 520 re.purge() # 娓呴櫎姝e垯琛ㄨ揪寮忕紦瀛?/span> # --- 姝e垯琛ㄨ揪寮忓璞?--- pat = re.compile(r"e") # 缂栬瘧鎴愭鍒欏璞?/span> # 鍖归厤妯″紡鏈?浣跨敤|缁勫悎): re.DEBUG / re.I(IGNORECASE)涓嶅尯鍒嗗ぇ灏忓啓 / re.L(LOCALE) / re.M(MULTILINE) / re.S(DOTALL) 鍖归厤浠讳綍瀛楃, 鈥?鈥樺惈鎹㈣, 娌℃湁姝ゆ爣蹇? 鈥?鈥橀櫎鎹㈣绗?/ re.U(UNICODE) / re.X(VERBOSE) pat = re.compile(r"e", re.S|re.I) index = pat.search(data) # 鏌ユ壘鍗曚釜鍖归厤椤?=> span=(2, 3), match=鈥榚鈥?/span> index = pat.search(data, 5) # => span=(10, 11), match=鈥榚鈥?/span> index = pat.search(data, 1, 10) index = pat.match(data) # 鍖归厤寮€澶?=> None index = pat.match(data, 2) # => span=(2, 3), match=鈥榚鈥?/span> index = pat.match(data, 1, 10) index = pat.fullmatch(data) # 鍖归厤鍏ㄩ儴 => None index = pat.fullmatch(data, 2) # => None index = pat.fullmatch(data, 2, 3) # span=(2, 3), match=鈥榚鈥?/span> lists = pat.split(data, maxsplit=0) # 鍒嗗壊 => [鈥楽h鈥? 鈥?is mor鈥? 鈥?than pr鈥? 鈥榯ty. 520鈥榏 lists = pat.findall(data) # 鏌ユ壘鍏ㄩ儴 => [鈥榚鈥? 鈥榚鈥? 鈥榚鈥榏 lists = pat.findall(data, 5) # => [鈥榚鈥? 鈥榚鈥榏 lists = pat.findall(data, 1, 10) # => [鈥榚鈥榏 strs = pat.sub(r"o", data, count=0) # 鏇挎崲 => Sho is moro than protty. 520 # --- Match --- match = index; # span=(2, 3), match=鈥榚鈥?/span> strs = match.string # 琚尮閰嶇殑鏁版嵁 => She is more than pretty. 520 strs = match.group() # 鑾峰彇 match(鍖归厤鍒扮殑) 鏁版嵁 => e pos = match.pos # 璧峰鍧愭爣 pos = match.endpos # 缁撴潫鍧愭爣 index.span()[0] # match(鍖归厤鍒?鐨勮捣濮嬪潗鏍?=> 2 index.span()[1] # match鐨勭粨鏉熷潗鏍?=> 3 if __name__ == "__main__": regular()
以上是关于姝e垯琛ㄨ揪寮忕殑浣跨敤的主要内容,如果未能解决你的问题,请参考以下文章