The core problem with all string matching algorithms is how to move the pattern string backwards when a mismatch occurs
First, the violent matching algorithm
If you want to match a string s and a pattern string p, then start with i=0 to match s[i: (I+len (P))], simple and rude, the code is as follows:
def matcher (T, p): # param t:the String to check # param P:pattern n = len (t) = len (p) for in xrange (0, n-m+1): if return True
Second, KMP algorithm
See also: http://blog.csdn.net/v_july_v/article/details/7041827
In simple terms, when matching the string s and pattern string p, when s[i] and P[j] do not match, do not backtrack s, but will shift p to a certain number of digits to start matching. The right shift number is determined by the following rules: if P[J] precedes the same string length as the maximum length of the string L, then move right (matched string length-L), text description is more abstract, see the above blog content
defPMT (s):"""partialmatchtable"""prefix= [S[:i+1] forIinchRange (len (s)-1)] Postfix= [S[i+1]: forIinchRange (len (s)-1)] Intersection= List (set (prefix) & Set (Postfix))#get the same prefix ifintersection:returnLen (Intersection[0])#get the longest prefix returnodefKMP (T, p):#t:the string to check #P:patterni =0 whileI < Len (t)-Len (p) + 1: Match=True forJinchRange (len (P)):ifT[I+J]! =P[j]: Match=False Break ifmatch:returnTrue#KMP ifj:i+ = J-PMT (P[:j])Else: i + = 1returnFalse
The above code reference http://cnblogs.com/goodspeed/p/3295456.html
In addition, there are BM algorithm, Sunday algorithm and Horspool algorithm, the latter two are the variants in the migration, "BM algorithm in practical applications than KMP algorithm three to five times times faster."
Python implementations of string matching