Break this out into a reusable function and document regex shortcomings.