我正在使用Ruby on Rails 3.2.9和Ruby 1.9.3。给定一个字符串,我想删除该字符串末尾的所有 /
字符。也就是说,例如:
From "abc/" to "abc"
From "abc//" to "abc"
From "abc///" to "abc"
...
From "a/b/c///" to "a/b/c"
我怎样才能做到(可能是使用正则表达式)?
我试过了:
string = string[0,string.length-1] if string.end_with?('/')
但它仅适用于一个角色。
答案 0 :(得分:5)
"abc//".sub %r{/+$}, ''
这只会删除字符串末尾的/
。
我使用%r{}
制作正则表达式,因为在这种情况下我不必转义/
。
答案 1 :(得分:2)
有些基准是有序的。
因为Perry Horwich的回答与Huluk的答案基本相同,所以我认为他们的结果会相同。嘿,我错了。在查看它并意识到它正在进行字符串搜索而不是正则表达式之后,我修复了它并添加了固定测试。
在那之后,我做了一些摆弄,看看我能想出什么。我的解决方案似乎并不像其他解决方案那么简单,但是第一个解决方案对于短字符串同样快速地完成工作,在长字符串上可能会快一点。在处理短弦或长弦时,后两者是冲洗,但两者都非常快。我恢复了老式的技术,并且做了他们如何在“回到白天”的汇编程序中做到这一点。
require 'benchmark'
n = 1_000_000
def huluk(s)
s.sub %r{/+$}, ''
end
def apneadiving1(your_string)
matches = your_string.match(/(.*?)\/*$/)
matches[1]
end
def apneadiving2(your_string)
your_string.scan(/(.*?)\/*$/).flatten.first
end
def fixed_perry_horwich(my_beginning_string)
my_beginning_string.sub(/\/+$/,'')
end
def the_tin_man1(s)
s.split(%r[/+$]).first
end
def the_tin_man2(s)
s = s[0..-2] while s[-1] == '/'
s
end
def the_tin_man3(s)
s_len = s.length - 1
s_len -= 1 while s[s_len] == '/'
s[0..s_len]
end
puts "Ruby #{ RUBY_VERSION }"
puts "#{ n } iterations"
puts
[
'abc//',
'abc' * 50 + '//',
'abc' * 10 + '////',
'////////////'
].each do |sample_string|
puts "sample string: #{ sample_string }"
puts 'huluk: %s' % huluk(sample_string)
puts 'perry_horwich: %s' % perry_horwich(sample_string)
puts 'fixed_perry_horwich: %s' % fixed_perry_horwich(sample_string)
puts 'apneadiving1: %s' % apneadiving1(sample_string)
puts 'apneadiving2: %s' % apneadiving2(sample_string)
puts 'the_tin_man1: %s' % the_tin_man1(sample_string)
puts 'the_tin_man2: %s' % the_tin_man2(sample_string)
puts 'the_tin_man3: %s' % the_tin_man3(sample_string)
puts
Benchmark.bm(19) do |b|
2.times do
b.report('huluk') { n.times { huluk(sample_string) } }
b.report('fixed_perry_horwich') { n.times { fixed_perry_horwich(sample_string) } }
b.report('apneadiving1') { n.times { apneadiving1(sample_string) } }
b.report('apneadiving2') { n.times { apneadiving2(sample_string) } }
b.report('the_tin_man1') { n.times { the_tin_man1(sample_string) } }
b.report('the_tin_man2') { n.times { the_tin_man2(sample_string) } }
b.report('the_tin_man3') { n.times { the_tin_man3(sample_string) } }
puts
end
end
puts
end
结果:
Ruby 1.9.3 1000000 iterations
sample string: abc// huluk: abc perry_horwich: abc// fixed_perry_horwich: abc apneadiving1: abc apneadiving2: abc the_tin_man1: abc the_tin_man2: abc the_tin_man3: abc user system total real huluk 1.840000 0.000000 1.840000 ( 1.849929) fixed_perry_horwich 1.840000 0.010000 1.850000 ( 1.847069) apneadiving1 1.900000 0.010000 1.910000 ( 1.900817) apneadiving2 4.860000 0.010000 4.870000 ( 4.883103) the_tin_man1 1.830000 0.000000 1.830000 ( 1.826547) the_tin_man2 1.150000 0.000000 1.150000 ( 1.151420) the_tin_man3 1.170000 0.010000 1.180000 ( 1.168161) huluk 1.820000 0.000000 1.820000 ( 1.825440) fixed_perry_horwich 1.830000 0.000000 1.830000 ( 1.827306) apneadiving1 1.850000 0.000000 1.850000 ( 1.853751) apneadiving2 4.830000 0.000000 4.830000 ( 4.825394) the_tin_man1 1.810000 0.000000 1.810000 ( 1.811503) the_tin_man2 1.150000 0.000000 1.150000 ( 1.151865) the_tin_man3 1.170000 0.000000 1.170000 ( 1.172259)
sample string: abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc// huluk: abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc perry_horwich: abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc// fixed_perry_horwich: abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc apneadiving1: abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc apneadiving2: abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc the_tin_man1: abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc the_tin_man2: abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc the_tin_man3: abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc user system total real huluk 2.120000 0.000000 2.120000 ( 2.122156) fixed_perry_horwich 2.130000 0.010000 2.140000 ( 2.136665) apneadiving1 9.630000 0.030000 9.660000 ( 9.656817) apneadiving2 12.420000 0.030000 12.450000 ( 12.459432) the_tin_man1 2.080000 0.010000 2.090000 ( 2.090112) the_tin_man2 1.570000 0.010000 1.580000 ( 1.573887) the_tin_man3 1.380000 0.000000 1.380000 ( 1.388120) huluk 2.130000 0.010000 2.140000 ( 2.134414) fixed_perry_horwich 2.120000 0.010000 2.130000 ( 2.129399) apneadiving1 9.620000 0.020000 9.640000 ( 9.646624) apneadiving2 12.410000 0.020000 12.430000 ( 12.423327) the_tin_man1 2.080000 0.010000 2.090000 ( 2.090938) the_tin_man2 1.580000 0.010000 1.590000 ( 1.581131) the_tin_man3 1.390000 0.000000 1.390000 ( 1.400093)
sample string: abcabcabcabcabcabcabcabcabcabc//// huluk: abcabcabcabcabcabcabcabcabcabc perry_horwich: abcabcabcabcabcabcabcabcabcabc//// fixed_perry_horwich: abcabcabcabcabcabcabcabcabcabc apneadiving1: abcabcabcabcabcabcabcabcabcabc apneadiving2: abcabcabcabcabcabcabcabcabcabc the_tin_man1: abcabcabcabcabcabcabcabcabcabc the_tin_man2: abcabcabcabcabcabcabcabcabcabc the_tin_man3: abcabcabcabcabcabcabcabcabcabc user system total real huluk 1.980000 0.010000 1.990000 ( 1.983149) fixed_perry_horwich 1.970000 0.010000 1.980000 ( 1.979796) apneadiving1 3.500000 0.010000 3.510000 ( 3.513885) apneadiving2 6.320000 0.020000 6.340000 ( 6.348327) the_tin_man1 2.070000 0.010000 2.080000 ( 2.075045) the_tin_man2 2.800000 0.000000 2.800000 ( 2.801136) the_tin_man3 1.840000 0.010000 1.850000 ( 1.851506) huluk 1.980000 0.010000 1.990000 ( 1.980867) fixed_perry_horwich 1.980000 0.010000 1.990000 ( 1.984776) apneadiving1 3.510000 0.010000 3.520000 ( 3.523383) apneadiving2 6.340000 0.020000 6.360000 ( 6.357442) the_tin_man1 2.070000 0.000000 2.070000 ( 2.074559) the_tin_man2 2.800000 0.010000 2.810000 ( 2.814275) the_tin_man3 1.850000 0.010000 1.860000 ( 1.855621)
sample string: //////////// huluk: perry_horwich: //////////// fixed_perry_horwich: apneadiving1: apneadiving2: the_tin_man1: the_tin_man2: the_tin_man3: user system total real huluk 1.980000 0.010000 1.990000 ( 1.982700) fixed_perry_horwich 1.980000 0.010000 1.990000 ( 1.984354) apneadiving1 1.860000 0.000000 1.860000 ( 1.869155) apneadiving2 4.840000 0.020000 4.860000 ( 4.852445) the_tin_man1 2.010000 0.000000 2.010000 ( 2.020292) the_tin_man2 5.060000 0.020000 5.080000 ( 5.070202) the_tin_man3 6.390000 0.020000 6.410000 ( 6.412697) huluk 1.980000 0.010000 1.990000 ( 1.987652) fixed_perry_horwich 1.980000 0.010000 1.990000 ( 1.981360) apneadiving1 1.860000 0.000000 1.860000 ( 1.871577) apneadiving2 4.850000 0.020000 4.870000 ( 4.857329) the_tin_man1 2.010000 0.010000 2.020000 ( 2.023877) the_tin_man2 5.060000 0.010000 5.070000 ( 5.067753) the_tin_man3 6.410000 0.020000 6.430000 ( 6.434523)
我试图将"abc//"
的简单案例和我认为是152个字符的“恶化案例”括起来,以对抗所提出的模式和算法。
huluk
和fixed_perry_horwich
是更快的,因为它们之间的差异被编译掉,因为Ruby会解释源。
编辑:
一些改变:
perry_horwich
测试,因为测试它没有任何意义。'////////////'
的测试。它显示了剥离分隔符的最坏情况测试的结果。测试结果的结果是查看正在测试的数据类型,并选择最适合输入的算法。对某些输入有效的方法并不一定适用于其他输入。我发现有趣的是,“老派”方法使用正则表达式来缩短尾随分隔符字符串。我认为这是Ruby设置对编译代码的调用的结果。一旦分隔符字符串变长,编译后的代码就会有优势。
答案 2 :(得分:1)
使用正则表达式检索字符串的预期部分:
/(.*?)\/*$/
见Rubular。
reg = /(.*?)\/*$/
matches = your_string.match(reg)
result = matches[1]
#or
result = your_string.scan(reg).flatten.first
答案 3 :(得分:0)
这适用于您的示例:
my_no_slash_terminated_string = my_beginning_string.sub(/\/+$/,'')
考虑是否要在字符串末尾或附近考虑空格。