skillfed

pyahocorasick

pyahocorasick is a fast and memory efficient library for exact or approximate multi-pattern string search. With the ``ahocorasick.Automaton`` class, you can find multiple key string occurrences at once in some input text. You can use it as a plain dict-like Trie or convert a Trie to an automaton for efficient Aho-Corasick search. And pickle to disk for easy reuse of large automatons. Implemented in C and tested on Python 3.6+. Works on Linux, macOS and Windows. BSD-3-Cause license.

pyahocorasick v2.3.1 6.0M downloads/30d#1,997 on PyPI1,122
Permissive license BSD-3-Clause and Public-Domain Active released

What it is and what it does

pyahocorasick is a C-based Python library implementing the Aho-Corasick string matching algorithm, allowing you to search for multiple keyword strings in text in a single pass. It exposes both a Trie data structure for dict-like key-value storage by string and a finalized automaton for efficient multi-pattern search. You build the automaton ahead of time, optionally pickle it to disk, then reuse it to search text repeatedly—making it suited for applications like intrusion detection, anti-virus scanning, or bioinformatics where you have a fixed set of patterns to match against variable input.

The library stores pattern prefixes efficiently in memory and guarantees similar worst-case and best-case runtime regardless of pattern count, depending mainly on input text size and match count. It supports both unicode and bytes, works on Python 3.10 and up, and is available as pre-built wheels for Linux, macOS, and Windows, though building from source requires a C compiler.

Use it for:

  • Scan DNA sequencing reads for known CRISPR guide sequences or genetic markers in bioinformatics pipelines
  • Detect multiple malware signatures or intrusion patterns in network traffic or log files
  • Find all occurrences of a dictionary of keywords in large text documents for content filtering or analysis
  • Build a reusable index of product codes or identifiers to match against incoming transaction or inventory data
  • Implement fast spell-checking or autocorrect by matching input against a pre-built dictionary of valid words

Worth the install?

AI-flagged interpretation of the facts on this page — verify before relying

Finds multiple keyword strings in text efficiently using an Aho-Corasick automaton, built and pickled for reuse.

Yes, if you need to match multiple fixed patterns in text efficiently. The C implementation, active maintenance, permissive license, and zero security vulnerabilities make it reliable. Medium install friction is offset by pre-built wheels and performance gains over pure-Python alternatives for large-scale matching. Not necessary for single-pattern or occasional searches.

Install

pyahocorasick on PyPI

pip

pip install pyahocorasick

uv

uv add pyahocorasick

poetry

poetry add pyahocorasick

Installing pyahocorasick

Before you install

Medium install friction due to C compilation requirement, but actively maintained with recent release 109 days ago and 1122 GitHub stars. Pre-built wheels available for Python 3.10+ on Linux, macOS, and Windows.

License in practice

BSD-3-Clause and Public-Domain license. Permissive treatment allows commercial and private use with minimal restrictions.

Quickstart

pip install pyahocorasick

import ahocorasick
automaton = ahocorasick.Automaton()
automaton.add_word('he', (0, 'he'))
automaton.add_word('she', (1, 'she'))
automaton.make_automaton()
for end_index, value in automaton.iter('she'):
    print(end_index, value)

Requires C compiler to build from source; Python 3.10+ required. Pre-built wheels available for common platforms.

Verify before relying

  • Whether approximate matching (mentioned in description) is fully implemented or only exact matching is production-ready
  • Performance characteristics compared to other multi-pattern matching libraries in real-world scenarios

Package facts

License BSD-3-Clause and Public-Domain (permissive)
Python support supports the current Python release (>=3.10)
Install friction medium — platform-specific wheel
Runtime dependencies none
Maintenance actively maintained — 109 days since the last release
Last repo commit
First released
Downloads 5,979,577/month — #1,997 on PyPI (30-day window, as of 2026-08-14)
Known vulnerabilities none known (OSV.dev, checked 2026-08-14)

Evidence: pyahocorasick-2.3.1-cp310-cp310-macosx_10_9_universal2.whl; pyahocorasick-2.3.1-cp310-cp310-macosx_11_0_arm64.whl; pyahocorasick-2.3.1-cp310-cp310-manylinux2014_aarch64.manylinux_2_17_aarch64.whl; pyahocorasick-2.3.1-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.whl; pyahocorasick-2.3.1-cp310-cp310-musllinux_1_2_aarch64.whl; pyahocorasick-2.3.1-cp310-cp310-musllinux_1_2_x86_64.whl; pyahocorasick-2.3.1-cp310-cp310-win_amd64.whl; pyahocorasick-2.3.1-cp311-cp311-macosx_10_9_universal2.whl; pyahocorasick-2.3.1-cp311-cp311-macosx_11_0_arm64.whl; pyahocorasick-2.3.1-cp311-cp311-manylinux2014_aarch64.manylinux_2_17_aarch64.whl; pyahocorasick-2.3.1-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.whl; pyahocorasick-2.3.1-cp311-cp311-musllinux_1_2_aarch64.whl; pyahocorasick-2.3.1-cp311-cp311-musllinux_1_2_x86_64.whl; pyahocorasick-2.3.1-cp311-cp311-win_amd64.whl; pyahocorasick-2.3.1-cp312-cp312-macosx_10_13_universal2.whl; pyahocorasick-2.3.1-cp312-cp312-macosx_11_0_arm64.whl; pyahocorasick-2.3.1-cp312-cp312-manylinux2014_aarch64.manylinux_2_17_aarch64.whl; pyahocorasick-2.3.1-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.whl; pyahocorasick-2.3.1-cp312-cp312-musllinux_1_2_aarch64.whl; pyahocorasick-2.3.1-cp312-cp312-musllinux_1_2_x86_64.whl

Keywords: aho-corasick, trie, automaton, dictionary

Development Status :: 5 - Production/StableProgramming Language :: CProgramming Language :: Python :: 3Topic :: Software Development :: LibrariesTopic :: Text Editors :: Text Processing

Tags

multi-pattern string matchingaho-corasick automatontrie data structurefast keyword searchpattern matching librarydictionary-based text searchbulk string matching
string-matchingtriepattern-search

More Libraries packages