fix pattern matching and generalize [ci skip]