This sounds like essentially the same problem as
matching production rules (look for the RETE algorithm
and its successors).
There's one thing that troubles me, and doubtless it is
addressed in the semantics, but when you are matching
multiple messages, it's no longer clear which is the
*first* match.