Clean up regex to allow more flexibility and reduce duplicate code