Skip to content

Welcome to Tech by Example

Menu
  • Home
  • Posts
  • System Design Questions
Menu

Extract a URL from a string in Ruby Language

Posted on July 23, 2021July 23, 2021 by admin

Overview

URI module can be used to extract URL from a given string

https://ruby-doc.org/stdlib-2.5.1/libdoc/uri/rdoc/URI.html

We can use the Extract method of the URI module

https://ruby-doc.org/stdlib-2.5.1/libdoc/uri/rdoc/URI.html#method-c-extract

Below is the signature of the method

URI::extract(str[, schemes][,&blk])

Args

  • str – This is the input to string to extract multiple URLs from
  • schemes – This is used to limit extraction to limited URLs

Program

Let’s first see a program to extract a single URL

require 'uri'


input = "The webiste is https://techbyexample.com:8000/tutorials/intro"
urls = URI.extract(input)

puts(urls)

Output

https://techbyexample.com:8000/tutorials/intro

Let’s see another program to extract multiple URLs

require 'uri'


input = "The webiste is https://techbyexample.com:8000/tutorials/intro amd mail to mailto:contactus@techbyexample.com"
urls = URI.extract(input)

puts(urls)

Output

https://techbyexample.com:8000/tutorials/intro
mailto:contactus@techbyexample.com

If we want to restrict the scheme then that also can be done.

require 'uri'


input = "The webiste is https://techbyexample.com:8000/tutorials/intro amd mail to mailto:contactus@techbyexample.com"
urls = URI.extract(input, "https")

puts(urls)

Output

https://techbyexample.com:8000/tutorials/intro

In the above program, we provided the scheme as https which is why we have only one output

  • extract
  • ruby
  • url
  • ©2025 Welcome to Tech by Example | Design: Newspaperly WordPress Theme