diff --git a/bin/autojump_match.py b/bin/autojump_match.py index cf166b9..03095c7 100644 --- a/bin/autojump_match.py +++ b/bin/autojump_match.py @@ -20,14 +20,15 @@ else: def match_anywhere(needles, haystack, ignore_case=False): """ Matches needles anywhere in the path as long as they're in the same (but - not necessary consecutive) order. + not necessary consecutive) order, and the last needle is at the end. For example: needles = ['foo', 'baz'] - regex needle = r'.*foo.*baz.*' + regex needle = r'foo.*/.*baz[^/]*$' haystack = [ (path='/foo/bar/baz', weight=10), (path='/baz/foo/bar', weight=10), + (path='/foo/baz/bar', weight=11), (path='/foo/baz', weight=10), ] @@ -36,8 +37,9 @@ def match_anywhere(needles, haystack, ignore_case=False): (path='/foo/baz', weight=10), ] """ - regex_needle = '.*' + '.*'.join(imap(re.escape, needles)) + '.*' - regex_flags = re.IGNORECASE | re.UNICODE if ignore_case else re.UNICODE + regex_some_seps = '.*' + os.sep + '.*' + regex_no_sep_end = '[^' + os.sep + ']*$' + regex_needle = regex_some_seps.join(imap(re.escape, needles)) + regex_no_sep_end found = lambda haystack: re.search( regex_needle, haystack.path,