Last active
December 20, 2015 14:59
-
-
Save dobrokot/6150988 to your computer and use it in GitHub Desktop.
fuzzy search regexp generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# usage: | |
# LC_ALL=C grep "$(python fuzzy_search.py "HELLO")" input.file.utf8.txt | |
import sys | |
s = sys.argv[1] | |
su = s.decode('utf-8') | |
utf8_any = '[^\x80-\xbf][\x80-\xbf]*' | |
#utf8_any = '.' | |
vars = [] | |
def escape_char(c): | |
if c in '.[]*\\$^': | |
return '\\' + c | |
return c | |
def esc(r): | |
return ''.join(map(escape_char, r)).encode('UTF-8') | |
for i in xrange(len(su)): | |
vars.append(esc(su[:i]) + utf8_any + esc(su[i+1:])) | |
vars.append(esc(su[:i]) + esc(su[i+1:])) | |
if (i != 0): | |
vars.append(esc(su[:i]) + utf8_any + esc(su[i:])) | |
sys.stdout.write('\\|'.join(vars)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@dobrokot, makes sense, thanks