factor out all the duplicated regex code into a wrapper.
Merge remote-tracking branch 'upstream/master'
merge with upstream