# Regular expression to parse out "host" part of URL string

**URL:** <https://www.ruby-forum.com/t/regular-expression-to-parse-out-host-part-of-url-string/57996>\
**Category:** Ruby\
**Created:** [April 17, 2006, 5:39pm UTC](https://www.ruby-forum.com/t/regular-expression-to-parse-out-host-part-of-url-string/57996 "2006-04-17T17:39:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [April 17, 2006, 5:39pm UTC](https://www.ruby-forum.com/t/regular-expression-to-parse-out-host-part-of-url-string/57996/1 "2006-04-17T17:39:05Z")

</div>

All,

I am trying to write a regex to parse out the “host” part of a potential  
URL.

So if presented with

> **[Breaking News, Latest News and Videos | CNN](https://www.cnn.com)**
>
> View the latest news and breaking news today for U.S., world, weather, entertainment, politics and health at CNN.com.

I want to get

> **[Breaking News, Latest News and Videos | CNN](https://www.cnn.com)**
>
> View the latest news and breaking news today for U.S., world, weather, entertainment, politics and health at CNN.com.

If presented with

> **[Breaking News, Latest News and Videos | CNN](https://www.cnn.com/)**
>
> View the latest news and breaking news today for U.S., world, weather, entertainment, politics and health at CNN.com.

I want to get

> **[Breaking News, Latest News and Videos | CNN](https://www.cnn.com)**
>
> View the latest news and breaking news today for U.S., world, weather, entertainment, politics and health at CNN.com.

If presented with

[www.cnn.com/some/other/stuff](http://www.cnn.com/some/other/stuff)

I want to get

> **[Breaking News, Latest News and Videos | CNN](https://www.cnn.com)**
>
> View the latest news and breaking news today for U.S., world, weather, entertainment, politics and health at CNN.com.

This regex: /(._)?/_/ matches everything  
This regex: /(.\*)?// matches everything including the “/”

How can I just get the part of the string before the slash?

Thanks,  
Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [April 17, 2006, 5:42pm UTC](https://www.ruby-forum.com/t/regular-expression-to-parse-out-host-part-of-url-string/57996/2 "2006-04-17T17:42:24Z")

</div>

Hi –

On Tue, 18 Apr 2006, Wes G. wrote:

> If presented with  
> How can I just get the part of the string before the slash?  
> How about:

/[^/]+/ # match one or more non-slash characters

David

–  
David A. Black (removed\_email\_address@domain.invalid)  
Ruby Power and Light, LLC ([http://www.rubypowerandlight.com](http://www.rubypowerandlight.com))

“Ruby for Rails” PDF now on sale! [Ruby for Rails](http://www.manning.com/black)  
Paper version coming in early May!

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [April 17, 2006, 5:50pm UTC](https://www.ruby-forum.com/t/regular-expression-to-parse-out-host-part-of-url-string/57996/3 "2006-04-17T17:50:11Z")

</div>

That works well.

Can you explain to me why

/(.\*)?//

matched the text in front of the “/” AND the “/”

though

?

unknown wrote:

> Hi –
> 
> On Tue, 18 Apr 2006, Wes G. wrote:
> 
> > If presented with  
> > How can I just get the part of the string before the slash?  
> > How about:
> 
> /[^/]+/ # match one or more non-slash characters
> 
> David
> 
> –  
> David A. Black (removed\_email\_address@domain.invalid)  
> Ruby Power and Light, LLC ([http://www.rubypowerandlight.com](http://www.rubypowerandlight.com))
> 
> “Ruby for Rails” PDF now on sale! [Ruby for Rails](http://www.manning.com/black)  
> Paper version coming in early May!

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [April 17, 2006, 6:41pm UTC](https://www.ruby-forum.com/t/regular-expression-to-parse-out-host-part-of-url-string/57996/4 "2006-04-17T18:41:45Z")

</div>

Wes G. wrote:

> That works well.
> 
> Can you explain to me why
> 
> /(.\*)?//
> 
> matched the text in front of the “/” AND the “/” ?

The entire regexp matches everything up to the first slash _and_ the  
slash. Recall, _this_ is the regexp, and it matches a trailing slash:

```
 (.*)?\/

```

The first parenthesized group of the regex, however, matches only up to,  
but not including, the trailing slash. So, if the entire regex matches,  
you want to get portion of the match that corresponds to the first  
parenthesized group, which will be stored in $1:

irb(main):009:0\* (“[example.com/](http://example.com/)” =~ /(.\*)?//) && $1  
=\> “[example.com](http://example.com)”

Or, you can capture the regex-matching operation’s result as a MatchData  
object and query it to retrieve the desired portion:

irb(main):010:0\> if matchdata = /(.\*)?//.match(“[example.com/](http://example.com/)”)  
irb(main):011:1\> matchdata[1]  
irb(main):012:1\> end  
=\> “[example.com](http://example.com)”

Or, you can use the zero-width positive lookahead regexp extension to  
make sure the entire regexp matches only what you want. Then you can  
use the entire match as your result:

irb(main):024:0\* if matchdata = /.\*?(?=/)/.match(“[example.com/](http://example.com/)”)  
irb(main):025:1\> matchdata.to\_s  
irb(main):026:1\> end  
=\> “[example.com](http://example.com)”

Cheers,  
Tom

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [April 22, 2006, 11:00pm UTC](https://www.ruby-forum.com/t/regular-expression-to-parse-out-host-part-of-url-string/57996/5 "2006-04-22T23:00:33Z")

</div>

Hi –

On Tue, 18 Apr 2006, Wes G. wrote:

> > /[^/]+/ # match one or more non-slash characters
> 
> ?

Because you told it to 🙂 You’ve got a / in the pattern.

(Keep in mind that the _whole_ pattern matches, not just the part in  
parentheses. The parentheses are just for capturing submatches.)

David

–  
David A. Black (removed\_email\_address@domain.invalid)  
Ruby Power and Light, LLC ([http://www.rubypowerandlight.com](http://www.rubypowerandlight.com))

“Ruby for Rails” PDF now on sale! [Ruby for Rails](http://www.manning.com/black)  
Paper version coming in early May!
