2011-05-11 68 views
1

我想做一個簡單的字符串匹配,從字符串的前面搜索。有沒有更簡單的方法來做到這一點可能是一個內置的? (re似乎有點小題大做)簡單的字符串匹配

def matchStr(ipadr = '10.20.30.40', matchIP = '10.20.'): 
    if ipadr[0:len(matchIP)] == matchIP: return True 
    return False 

回答

7
def matchStr(ipadr = '10.20.30.40', matchIP = '10.20.'): 
    return ipadr.startswith(matchIP) 
+0

+1以10秒打我。 – Malvolio 2011-05-11 19:25:15

+0

+2:20秒 – Andrey 2011-05-11 19:26:55

+0

我知道必須有一個更簡單的方法。謝謝。 – tMC 2011-05-11 19:38:47

1
>>> 'abcde'.startswith('abc') 
True 
1
'10.20.30.40'.startswith('10.20.') 
>>>True