replace with pattern matching