python如何實現(xiàn)glob style pattern,相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。
成都創(chuàng)新互聯(lián)公司是專業(yè)的堆龍德慶網(wǎng)站建設(shè)公司,堆龍德慶接單;提供成都網(wǎng)站建設(shè)、網(wǎng)站建設(shè),網(wǎng)頁設(shè)計,網(wǎng)站設(shè)計,建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進行堆龍德慶網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團隊,希望更多企業(yè)前來合作!
一說起通配符,大家很快就會想起*和?號,有了通配符,使得表達能力大大增強,很多l(xiāng)inux命令都支持這個東西,其實就是glob style pattern.
就連redis的keys命令都支持glob.
我要實現(xiàn)的glob,支持以下特性:
星號*匹配0個或多個任意字符
?匹配確切的一個任意字符
[characters]匹配任意一個方括號內(nèi)的字符,比如[abc],要么匹配a,要么匹配b,要么匹配c.
[!character]排除方括號內(nèi)的字符
[character-character],表示2個字符范圍內(nèi)的都可以匹配,如[a-z],[0-9]
實現(xiàn)這個東西其實挺簡單的,從左往右掃描s串和p串,如果最后都走到了結(jié)尾,那么就是可以匹配的.
主要難點在于*號的匹配.因為*號可以匹配0個或者多個,所以需要試探回溯.這里通過保存*號位置,如果后面的走不通了,就拉回*號位置,貪婪匹配.
至于方括號的展開,弄個include和exclude變量就很清晰了.
下面上代碼.
#coding=utf-8 def build_expand(p):#方括號展開 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)
運行結(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如何實現(xiàn)glob style pattern的方法了嗎?如果還想學到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
文章名稱:python如何實現(xiàn)globstylepattern
地址分享:http://muchs.cn/article20/ihcpjo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、響應(yīng)式網(wǎng)站、建站公司、網(wǎng)站維護、移動網(wǎng)站建設(shè)、品牌網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)