The document describes an algorithm called CaffeineFix that uses finite state machines to efficiently recognize chemical names in text, even in the presence of errors. It can represent chemical dictionaries as finite state machines to enable fuzzy matching and automatic spelling correction. This improves the extraction of chemical structures from patents compared to other methods. A benchmark showed CaffeineFix matched or exceeded other tools on extracting structures for top selling drugs from patents.