I didn't attempt to parse that but there seems to be several constructs in there that would make me nervous. You have two '$' and only one '^' It appears you have two optional clauses. Optional clauses without hard anchors general are always a problem because they likely make the regex engine do a lot of work. Since you already presumably have a working solution what makes you think you need to combine them into one expression? Or another way of saying that is that regexes use an iterative process to find the best solution and more complex expressions unless carefully crafted can cause unexpected problems (slowness.)