need to detect earliest pattern match and execute corresponding expansion