Pattern recognition and matching