Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby Regular expression to match a url [duplicate]

Tags:

regex

url

ruby

Possible Duplicates:
Regex to match URL
regex to remove the webpage part of a url in ruby

I am in search of a regular expression for parsing all the urls in a file.
i tried many of the regular expression i got after googling but it fails in one or the other case . my idea is to write one which checks the presense of http or https at the begening and it will match everything untill it sees a blank space .
any ideas ?
NOTE : i dont need to parse the url but erase all the urls from a file or atleast make it unreadable .

like image 868
Krishna Prasad Varma Avatar asked Jan 17 '11 18:01

Krishna Prasad Varma


2 Answers

The standard URI library provides URI.regexp which is the regular expression for url string.

 require 'uri'
 string.scan(URI.regexp)

http://ruby-doc.org/stdlib/libdoc/uri/rdoc/index.html

like image 69
John Dyer Avatar answered Nov 07 '22 11:11

John Dyer


You can try this:

/https?:\/\/[\S]+/

The \S means any non-whitespace character.

(Rubular)

like image 45
Mark Byers Avatar answered Nov 07 '22 13:11

Mark Byers