Parsing HTML with a regex is never a good option, but it's sometimes the only option.
The more fundamental question for me is why the author didn't decide to make make code blocks non-breaking by default, or just add the class annotations when he writes the HTML?