Back-patch of commits 72dd6291, f6a05fd9, and 60e9c224. This is needed to support fixing the regex prefix extraction bug in back branches.