2016-10-12 1 views
0

入力は、2つ以上のスラッシュを含む可能性のあるURLです。私は以下の2つのコマンドでそれを修正しました。これはかなり読みやすい解決策と思われます。URL内のスラッシュを修正する

re.sub()コマンドを1つだけ実行しても同じ効果が得られるのだろうかと思います。

url = re.sub("/[/]+", "/", url) # two or more slashes replace with one slash 
url = re.sub("http:/", "http://", url) # correct one mistake of the previous command 

答えて

2

はいできます。負のルックバックマークを使用して?<!

print(re.sub('(?<!http:)//+', '/', 'http://httpbin.org//ip')) 
# http://httpbin.org/ip 
関連する問題