How to add a variable error to a fuzzy regular expression search. python

import regex,re


sequence = 'aaaaaaaaaaaabbbbbbbbbbbbcccccccccccc' #being searched
query = 'aaabbbbbbbbbbbbccc' #100% coverage
query_1 = 'aaaabbbbbbbbcbbbcccc' #95% coverage
query_2 = 'aaabbbbcbbbbbcbccc' #90% coverage

threshold = .95
error = len(query_1) - (len(query_1)*threshold) #for query_1 errors must be <= 1

print regex.search(query_1 + '{e<={}}'.format(error),sequence).group(0)

I am trying to add additional parameters to the search for regular expressions, so it only works if a certain percentage of the query is in the search.

For example, if I wanted it to occupy at least 95%, it would work for query_1, but it would not work forquery_2

+2
source share
1 answer

Using the module regex:

import regex
sequence = 'aaaaaaaaaaaabbbbbbbbbbbbcccccccccccc' #being searched
query = 'aaabbbbbbbbbbbbccc' #100% coverage
query_1 = 'aaaabbbbbbbbcbbbcccc' #95% coverage
query_2 = 'aaabbbbcbbbbbcbccc' #90% coverage
threshold = 0.97
queries = (query, query_1, query_2)
for q in queries:
    error = int(len(q) - (len(q)*threshold))
    m = regex.search(r'(%s){e<=%d}'%(q,error), sequence)
    print 'match' if m else 'nomatch'
+1
source

All Articles