Added InvalidPatternException and replacement changes