Reputation: 5313
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?
Upvotes: 5
Views: 3523
Reputation: 5313
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.
Upvotes: 4