longest match rewritten