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:
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/”)
irb(main):011:1> matchdata[1]
irb(main):012:1> end
=> “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/”)
irb(main):025:1> matchdata.to_s
irb(main):026:1> end
=> “example.com”