By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,089 Members | 2,260 Online
Bytes IT Community
+ Ask a Question
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
Share this Question
Share on Google+
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
  1. >>> string2 = "70%"
  2. >>> string1 = "50%"
  3. >>> string1[:-1]
  4. '50'
  5. >>> string2[:-1]
  6. '70'
  7. >>> if string1[:-1] == string2[:-1]:
  8. ...  print "Match"
  9. ...
  10. >>>                          
  11.  
if you want to compare the last character too, use the slice method again
Expand|Select|Wrap|Line Numbers
  1. string1[-1] == string2[-1]
  2.  
Dec 2 '07 #2

bvdet
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
  1. def wordCmp(word1, word2):
  2.     # pad word if unequal length
  3.     word2 = '%s%s' % (word2, ' '*(len(word1)-len(word2)))
  4.     word1 = '%s%s' % (word1, ' '*(len(word2)-len(word1)))
  5.     num = 0
  6.     for i, letter in enumerate(word1):
  7.         if not cmp(letter, word2[i]):
  8.             num += 1
  9.     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
  1. def wordCmp(word1, word2):
  2.     # pad word if unequal length
  3.     word2 = '%s%s' % (word2, ' '*(len(word1)-len(word2)))
  4.     word1 = '%s%s' % (word1, ' '*(len(word2)-len(word1)))
  5.     num = 0
  6.     for i, letter in enumerate(word1):
  7.         if not cmp(letter, word2[i]):
  8.             num += 1
  9.     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

Post your reply

Sign in to post your reply or Sign up for a free account.