Implement replace_rule()