如何得到下面的字符串来输出下面的内容?
regex = '' # help me out here guys!
'baz/foobaryfobar/'.match(regex) #=> foobaryfobar
'baz/foobar'.match(regex) #=> foobar
'baz/foobar.jpg'.match(regex) #=> foobar
'baz/foobar.png'.match(regex) #=> foobar
'baz/foobar.gif'.match(regex) #=> foobar
'baz/foobar.jpeg'.match(regex) #=> foobar
可以用一个正则表达式来完成吗?
更新
对此非常欢迎使用非正则表达式解决方案!
最佳答案
如果不使用 Regex,但使用 extname
怎么样? , basename
?
def gen_file_name(string)
File.basename(string,File.extname(string))
end
gen_file_name('baz/foobar.jpg') # => "foobar"
gen_file_name("baz/foobaryfobar/") # => "foobaryfobar"
gen_file_name('baz/foobar') # => "foobar"
关于ruby - 使用条件正则表达式从 url 生成文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22426619/