如何在Ruby字符串中返回最后一个斜杠(/)之后的所有内容

时间:2022-10-20 10:46:49

I have a string would like everything after the last / to be returned.

我有一根绳子,希望在最后一根绳子之后所有的东西都归还。

E.g. for https://www.example.org/hackerbob, it should return "hackerbob".

例如,对于https://www.example.org/hackerbob,它应该返回“hackerbob”。

7 个解决方案

#1


65  

I don't think a regex is a good idea, seeing how simple the task is:

我不认为regex是一个好主意,看看这个任务有多简单:

irb(main):001:0> s = 'https://www.facebook.com/hackerbob'
=> "https://www.facebook.com/hackerbob"
irb(main):002:0> s.split('/')[-1]
=> "hackerbob"

Of course you could also do it using regex, but it's a lot less readable:

当然你也可以使用regex来完成,但是它的可读性要差得多:

irb(main):003:0> s[/([^\/]+)$/]
=> "hackerbob"

#2


8  

Use the right tool for the job:

使用正确的工具完成工作:

require 'uri'
url = "https://www.facebook.com/hackerbob"
URI.parse(url).path[1..-1]  # => "hackerbob"

#3


2  

One more sample

一个样本

str = 'https://www.facebook.com/hackerbob'
ending = str.match(/.com\/(.*)/)
p ending[1]

#4


0  

May use this

可以使用这个

subject = /(\/)([^\/]+)\Z/i.match(subject)
if match
    # match start: match.begin(0)
    # match end (exclusive): match.end(0)
    # matched text: match[0]
    # backreference n start: match.begin(n)
    # backreference n end (exclusive): match.end(n)
    # backreference n text: match[n]
else
    # Match attempt failed
end

#5


0  

Use It

使用它

 1.9.3p194 :039 > line = 'https://www.facebook.com/hackerbob'
 1.9.3p194 :039 > line.match(/.com\/(\w+$)/)[1]  #    => "hackerbob"

#6


0  

Sure you can:

当然,您可以:

string = 'https://www.facebook.com/hackerbob'
string =~ /^https?:\/\/www\.facebook\.com\/(.*)/
$1

#7


0  

You can try /[a-zA-Z]+://[a-z]+.[a-z]+.[a-z]+/(.+)/ if you are using it for the string above or any other similar url. A great tool to use for regex is http://www.rubular.com

您可以尝试/[a-zA-Z]+://[a-z]+.[a-z]+.[a-z]+/(.+)/如果您使用它作为上面的字符串或任何其他类似url。regex的一个很好的工具是http://www.rubular.com。

#1


65  

I don't think a regex is a good idea, seeing how simple the task is:

我不认为regex是一个好主意,看看这个任务有多简单:

irb(main):001:0> s = 'https://www.facebook.com/hackerbob'
=> "https://www.facebook.com/hackerbob"
irb(main):002:0> s.split('/')[-1]
=> "hackerbob"

Of course you could also do it using regex, but it's a lot less readable:

当然你也可以使用regex来完成,但是它的可读性要差得多:

irb(main):003:0> s[/([^\/]+)$/]
=> "hackerbob"

#2


8  

Use the right tool for the job:

使用正确的工具完成工作:

require 'uri'
url = "https://www.facebook.com/hackerbob"
URI.parse(url).path[1..-1]  # => "hackerbob"

#3


2  

One more sample

一个样本

str = 'https://www.facebook.com/hackerbob'
ending = str.match(/.com\/(.*)/)
p ending[1]

#4


0  

May use this

可以使用这个

subject = /(\/)([^\/]+)\Z/i.match(subject)
if match
    # match start: match.begin(0)
    # match end (exclusive): match.end(0)
    # matched text: match[0]
    # backreference n start: match.begin(n)
    # backreference n end (exclusive): match.end(n)
    # backreference n text: match[n]
else
    # Match attempt failed
end

#5


0  

Use It

使用它

 1.9.3p194 :039 > line = 'https://www.facebook.com/hackerbob'
 1.9.3p194 :039 > line.match(/.com\/(\w+$)/)[1]  #    => "hackerbob"

#6


0  

Sure you can:

当然,您可以:

string = 'https://www.facebook.com/hackerbob'
string =~ /^https?:\/\/www\.facebook\.com\/(.*)/
$1

#7


0  

You can try /[a-zA-Z]+://[a-z]+.[a-z]+.[a-z]+/(.+)/ if you are using it for the string above or any other similar url. A great tool to use for regex is http://www.rubular.com

您可以尝试/[a-zA-Z]+://[a-z]+.[a-z]+.[a-z]+/(.+)/如果您使用它作为上面的字符串或任何其他类似url。regex的一个很好的工具是http://www.rubular.com。