64

文字列内のすべての URL を削除したい (それらを "" に置き換えます)

例:

text1
text2
http://url.com/bla1/blah1/
text3
text4
http://url.com/bla2/blah2/
text5
text6
http://url.com/bla3/blah3/

結果を次のようにしたい:

text1
text2
text3
text4
text5
text6
4

14 に答える 14

85

Python スクリプト:

import re
text = re.sub(r'^https?:\/\/.*[\r\n]*', '', text, flags=re.MULTILINE)

出力:

text1
text2
text3
text4
text5
text6

このコードをここでテストします。

于 2012-07-04T16:15:58.030 に答える
26

これは私のために働いた:

import re
thestring = "text1\ntext2\nhttp://url.com/bla1/blah1/\ntext3\ntext4\nhttp://url.com/bla2/blah2/\ntext5\ntext6"

URLless_string = re.sub(r'\w+:\/{2}[\d\w-]+(\.[\d\w-]+)*(?:(?:\/[^\s/]*))*', '', thestring)
print URLless_string

結果:

text1
text2

text3
text4

text5
text6
于 2012-07-04T16:12:43.740 に答える
6

反対側から見ることもできます...

from urlparse import urlparse
[el for el in ['text1', 'FTP://somewhere.com', 'text2', 'http://blah.com:8080/foo/bar#header'] if not urlparse(el).scheme]
于 2012-07-04T16:48:26.433 に答える