Configure MatchKind for overlapping patterns
mainWhen patterns overlap, you can control which match is returned using the matchkind argument in the constructor. This is supported by both AhoCorasick and BytesAhoCorasick.
Available MatchKind options:
MatchKind.Standard(Default): Returns the pattern that matches first semantically (the one that ends earliest in the haystack).MatchKind.LeftmostFirst: Returns the leftmost-in-the-haystack pattern that appears first in the provided list of patterns.MatchKind.LeftmostLongest: Returns the leftmost-in-the-haystack pattern that is the longest.
To get all overlapping matches, use MatchKind.Standard and set overlapping=True in the search method.
from ahocorasick_rs import AhoCorasick, MatchKind
# LeftmostFirst example
ac = AhoCorasick(["disco", "disc"], matchkind=MatchKind.LeftmostFirst)
print(ac.find_matches_as_strings("discontent")) # ['disco']
# Overlapping matches example
patterns = ["winter", "onte", "disco", "discontent"]
ac = AhoCorasick(patterns)
print(ac.find_matches_as_strings("discontent", overlapping=True))
# ['disco', 'onte', 'discontent']