Python implementations of string matching

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.