Pyparsing 3.2.2 cleans up a formerly internal-only utility method make_compressed_re(), and makes it part of the public API. Given a list of strings, it generates a regex to match those strings and only those (so no cheating like ".*").
Here are before/after expressions for matching chemical symbols.
I should have tagged that with #regex when I first posted it - doesn't look like adding a tag in an edit accomplishes much.
I hope to release pyparsing 3.2.2 this weekend.
This is not an exhaustive optimizer by any means - it does a poor job with the days of the week in English, even though they all end in "day".