444,089 Members | 2,260 Online Need help? Post your question and get tips & solutions from a community of 444,089 IT Pros & Developers. It's quick & easy.

# matching x% of a string using REs?

 P: 56 I was wondering if it is possible to match string1 to x% of string2 using a re. I'm not sure how I would implement this exactly if it can be done, but what im ultimately looking at is it being able to detect if a string matches another string to like 70% (made some typos?) Also, do you experts have any links for where I can understand how REs work, in python, and more exactly, in general? so far I've been using this: http://www.amk.ca/python/howto/regex/ Would appreciate any input, thanks ~William Dec 1 '07 #1
3 Replies

 Expert 100+ P: 511 I was wondering if it is possible to match string1 to x% of string2 using a re. I'm not sure how I would implement this exactly if it can be done, but what im ultimately looking at is it being able to detect if a string matches another string to like 70% (made some typos?) Also, do you experts have any links for where I can understand how REs work, in python, and more exactly, in general? so far I've been using this: http://www.amk.ca/python/howto/regex/ Would appreciate any input, thanks ~William regexp are the last thing that come to my mind. Expand|Select|Wrap|Line Numbers >>> string2 = "70%" >>> string1 = "50%" >>> string1[:-1] '50' >>> string2[:-1] '70' >>> if string1[:-1] == string2[:-1]: ...  print "Match" ... >>>                             if you want to compare the last character too, use the slice method again Expand|Select|Wrap|Line Numbers string1[-1] == string2[-1]   Dec 2 '07 #2

 Expert Mod 2.5K+ P: 2,851 I was wondering if it is possible to match string1 to x% of string2 using a re. I'm not sure how I would implement this exactly if it can be done, but what im ultimately looking at is it being able to detect if a string matches another string to like 70% (made some typos?) Also, do you experts have any links for where I can understand how REs work, in python, and more exactly, in general? so far I've been using this: http://www.amk.ca/python/howto/regex/ Would appreciate any input, thanks ~William I am no RE expert, but I think another way would be more feasible. You can do a simple word compare between two strings, and establish x% based on the number of characters of each word that matches versus the number of characters to compare. You need to establish rules how it would work. Should you ignore case? Are the strings in particular formats? Should you ignore punctuation or whitespace? If two words do not match, would you start comparing individual letters? The following seems a bit crude: Expand|Select|Wrap|Line Numbers def wordCmp(word1, word2):     # pad word if unequal length     word2 = '%s%s' % (word2, ' '*(len(word1)-len(word2)))     word1 = '%s%s' % (word1, ' '*(len(word2)-len(word1)))     num = 0     for i, letter in enumerate(word1):         if not cmp(letter, word2[i]):             num += 1     return num, max(len(word1), len(word2)) Maybe it will give you some ideas. Dec 2 '07 #3

 P: 56 Expand|Select|Wrap|Line Numbers def wordCmp(word1, word2):     # pad word if unequal length     word2 = '%s%s' % (word2, ' '*(len(word1)-len(word2)))     word1 = '%s%s' % (word1, ' '*(len(word2)-len(word1)))     num = 0     for i, letter in enumerate(word1):         if not cmp(letter, word2[i]):             num += 1     return num, max(len(word1), len(word2)) Maybe it will give you some ideas. It did indeed give me some ideas, and they work for what I needed, thank you. Dec 3 '07 #4 