28

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 .

3
  • P.S.: where do you see Rails here? I delete this tag. Do you know difference between Ruby and Rails? Commented Jan 17, 2011 at 18:37
  • Yeah, the possible duplicates questions are hardly duplicates although one could find the good answer there URI.parse or URI::DEFAULT_PARSER.make_regexp. And I don't even see a reopen vote here. Commented Jan 28, 2023 at 18:08
  • This one is for rails but still not rails only answers: stackoverflow.com/q/161738/520567 Commented Jan 28, 2023 at 18:08

2 Answers 2

69

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

Sign up to request clarification or add additional context in comments.

5 Comments

NB there is also a URI.extract method which basically parses all url's out of a string. Very useful.
undefined local variable or method `string' for main:Object (NameError) Did you mean? String
URI.regexp seems to match strings that aren't really valid URLs such as http://x, or even just http:.
string is the variable you create, @FeloVilches
This matches things like C: too
26

You can try this:

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

The \S means any non-whitespace character.

(Rubular)

3 Comments

Is that really a uri regex though? That's just looking for any string that starts with http(s)... which is not a uri.
It won't work for the url [https://google.com//]. [ is not a valid url character.
It won't work for urls with quotes as it will include the quotes

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.