Donny Kurnia
Donny Kurnia

Reputation: 5313

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?

Upvotes: 5

Views: 3523

Answers (2)

Donny Kurnia
Donny Kurnia

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

The Mask
The Mask

Reputation: 17427

Try with regex:

(?:f|ht)tps?:\/[^\s]+

Upvotes: 9

Related Questions