factor out all the duplicated regex code into a wrapper.