import re
p = re.compile(ur'((\s|\A)((ht|f)tp(s)?://)?(www\.)?([\w\-\.]+)\.\w+([\w\-\./\?#]+)?\b)')
test_str = u"I visited http://www.google.com."
print(re.sub(p, lambda x: x.group(0).replace(" ","~"), test_str))