Set regex's encoding to string's encoding when possible.