python如何實(shí)現(xiàn)glob style pattern,相信很多沒(méi)有經(jīng)驗(yàn)的人對(duì)此束手無(wú)策,為此本文總結(jié)了問(wèn)題出現(xiàn)的原因和解決方法,通過(guò)這篇文章希望你能解決這個(gè)問(wèn)題。
成都創(chuàng)新互聯(lián)公司專注于常熟企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站建設(shè),商城網(wǎng)站定制開(kāi)發(fā)。常熟網(wǎng)站建設(shè)公司,為常熟等地區(qū)提供建站服務(wù)。全流程按需求定制開(kāi)發(fā),專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,成都創(chuàng)新互聯(lián)公司專業(yè)和態(tài)度為您提供的服務(wù)一說(shuō)起通配符,大家很快就會(huì)想起*和?號(hào),有了通配符,使得表達(dá)能力大大增強(qiáng),很多l(xiāng)inux命令都支持這個(gè)東西,其實(shí)就是glob style pattern.
就連redis的keys命令都支持glob.
我要實(shí)現(xiàn)的glob,支持以下特性:
星號(hào)*匹配0個(gè)或多個(gè)任意字符
?匹配確切的一個(gè)任意字符
[characters]匹配任意一個(gè)方括號(hào)內(nèi)的字符,比如[abc],要么匹配a,要么匹配b,要么匹配c.
[!character]排除方括號(hào)內(nèi)的字符
[character-character],表示2個(gè)字符范圍內(nèi)的都可以匹配,如[a-z],[0-9]
實(shí)現(xiàn)這個(gè)東西其實(shí)挺簡(jiǎn)單的,從左往右掃描s串和p串,如果最后都走到了結(jié)尾,那么就是可以匹配的.
主要難點(diǎn)在于*號(hào)的匹配.因?yàn)?號(hào)可以匹配0個(gè)或者多個(gè),所以需要試探回溯.這里通過(guò)保存*號(hào)位置,如果后面的走不通了,就拉回*號(hào)位置,貪婪匹配.
至于方括號(hào)的展開(kāi),弄個(gè)include和exclude變量就很清晰了.
下面上代碼.
#coding=utf-8 def build_expand(p):#方括號(hào)展開(kāi) ptr2include = {} ptr2exclude = {} ptr2next = {} len_p = len(p) pPtr = 0 while pPtr<len_p: if p[pPtr] == '[': start = pPtr pPtr += 1 include = set([]) exclude = set([]) while p[pPtr]!=']': if p[pPtr]=='!': exclude.add(p[pPtr+1]) pPtr += 2 elif p[pPtr+1] == '-': include.update({chr(x) for x in range(ord(p[pPtr]),ord(p[pPtr+2])+1)}) pPtr += 3 else: include.add(p[pPtr]) pPtr += 1 if include: ptr2include[start] = include if exclude: ptr2exclude[start] = exclude ptr2next[start] = pPtr + 1 else: pPtr += 1 return ptr2include, ptr2exclude, ptr2next def isMatch(s, p): len_s = len(s); len_p = len(p) sPtr = pPtr = ss = 0 star = None ptr2include, ptr2exclude, ptr2next = build_expand(p) while sPtr<len_s: if pPtr<len_p and (p[pPtr] in ['?',s[sPtr]]): sPtr += 1; pPtr += 1 continue if pPtr<len_p and p[pPtr] == '[': if pPtr in ptr2include and s[sPtr] in ptr2include[pPtr]: sPtr += 1 pPtr = ptr2next[pPtr] continue if pPtr in ptr2exclude and s[sPtr] not in ptr2exclude[pPtr]: sPtr += 1 pPtr = ptr2next[pPtr] continue if pPtr<len_p and p[pPtr]=='*': star = pPtr; pPtr += 1; ss = sPtr continue if star is not None: pPtr = star + 1; ss += 1; sPtr = ss continue return False while pPtr<len(p) and p[pPtr]=='*': pPtr += 1 return pPtr == len_p if __name__ == '__main__': params = [ ("aa","a"), ("aa","aa"), ("aaa","aa"), ("aa", "*"), ("aa", "a*"), ("ab", "?*"), ("aab", "c*a*b"), ("cab", "c*a*b"), ("cxyzbazba", "c*ba"), ('abc','ab[a-c]'), ('abd','ab[a-c]'), ('abe','ab[cde]'), ('abe','ab[!e]'), ('abe','ab[!c]'), ] for p in params: print p,isMatch(*p)
運(yùn)行結(jié)果是
('aa', 'a') False
('aa', 'aa') True
('aaa', 'aa') False
('aa', '*') True
('aa', 'a*') True
('ab', '?*') True
('aab', 'c*a*b') False
('cab', 'c*a*b') True
('cxyzbazba', 'c*ba') True
('abc', 'ab[a-c]') True
('abd', 'ab[a-c]') False
('abe', 'ab[cde]') True
('abe', 'ab[!e]') False
('abe', 'ab[!c]') True
看完上述內(nèi)容,你們掌握python如何實(shí)現(xiàn)glob style pattern的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
分享文章:python如何實(shí)現(xiàn)globstylepattern-創(chuàng)新互聯(lián)
分享地址:http://muchs.cn/article40/dhegho.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、云服務(wù)器、網(wǎng)站內(nèi)鏈、ChatGPT、網(wǎng)站設(shè)計(jì)公司、靜態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容