Regex-style pattern-matching for time series This is more of a "what technology/library would you use for this?" question than anything else.

I have categorical time series data, and need to match cases in these time series to known patterns. For example, State A, followed by State B within six months, followed by multiple periods of State A, followed by an optional period of State B, followed by State C.

It's quite hard to interrogate e.g. an SQL database or pandas dataframe for sequences of rows that match a pattern like this. How you'd do this "in theory" is build a pattern-matching finite state machine, but I didn't know a relevant library for one. Then I realised I've been using pattern-matching finite state machines for years in the form of regular expressions.

So, what I've been doing is converting my time series to strings, defining my pattern as a regex, and matching them that way. It works extraordinarily well. In spite of this, it feels hacky. Surely there should be some proper way to do this.

Is there? What technology/data structure/library would let me define finite automata for time series and match them based on that definition?
If you need optional parts in the pattern then regular expressions were specifically built for those kind of problems. You could probably speed up regular expressions by using loops in Cython, but the time spent programming it is not really worth it.

If your pattern is fixed then there are faster ways to do pattern matching. OpenCV uses Descrete Fourier Transformation to match a template. It is very fast and is available for Python and C++. It is optimized to work with 2D images, and can also work with 32 bit floats. How DFT works

Documentation for template matching in OpenCV