move safe regex logic to it's own place, + reusability