Removing url from text using ruby
Given a text, I 开发者_如何学运维want to remove the url part and leave other text.
Example:
'bla bla bla... bla bla bla... http://bit.ly/someuri bla bla bla...'
to become
'bla bla bla... bla bla bla... bla bla bla...'
Is there any ruby build in method to do this efficiently?
Try with regex:
(?:f|ht)tps?:\/[^\s]+
I just found Regular Expression - replace word except within a URL/URI and modify the code to be like this:
URI_REGEX = %r"((?:(?:[^ :/?#]+):)(?://(?:[^ /?#]*))(?:[^ ?#]*)(?:\?(?:[^ #]*))?(?:#(?:[^ ]*))?)"
def remove_uris(text)
text.split(URI_REGEX).collect do |s|
unless s =~ URI_REGEX
s
end
end.join
end
I test it in rails console and it worked as expected:
remove_uris('bla bla bla... bla bla bla... http://bit.ly/someuri bla bla bla...')
=> "bla bla bla... bla bla bla... bla bla bla..."
If anyone have better / effective solution, I will vote up or accept it. Thanks.
精彩评论