Ruby regexp中的可选空白
我想创建一个regexp来指定括号之间的单词。例如,我有这样一个字符串:Ruby regexp中的可选空白,ruby,regex,Ruby,Regex,我想创建一个regexp来指定括号之间的单词。例如,我有这样一个字符串: "something(a,b)" 及 我想从括号中提取字母 在第一个字符串中,我想得到一个数组['a','b']。在第二个例子中,我需要数组['c','d'] 我有以下方法: def suffixes(t) (t.scan /\((\w+),(\w+)\)/).flatten end 但这只适用于第一种情况。在第二种变体中,我有: def suffixes(t) (t.scan /\((\w+),[\s](\w
"something(a,b)"
及
我想从括号中提取字母
在第一个字符串中,我想得到一个数组['a','b']
。在第二个例子中,我需要数组['c','d']
我有以下方法:
def suffixes(t)
(t.scan /\((\w+),(\w+)\)/).flatten
end
但这只适用于第一种情况。在第二种变体中,我有:
def suffixes(t)
(t.scan /\((\w+),[\s](\w+)\)/).flatten
end
但这只适用于第二种情况。我不知道在这两种情况下,什么regexp将运行。您可以使用:
def suffixes(t)
(t.scan /\((\w+)\s*,\s*(\w+)\)/).flatten
end
\s*
将在逗号前后匹配0个或多个空格。您可以使用:
def suffixes(t)
(t.scan /\((\w+)\s*,\s*(\w+)\)/).flatten
end
\s*
将在逗号前后匹配0个或多个空格。将中间的\s
设置为可选
def suffixes(t)
(t.scan /\((\w+),\s?(\w+)\)/).flatten
end
?
之后的\s
将把空格变为可选(0或1)。将中间\s
设为可选
def suffixes(t)
(t.scan /\((\w+),\s?(\w+)\)/).flatten
end
?
之后的\s
会将空格变为可选(0或1)
试试这个。这将适用于所有参数。请参阅演示
试试这个。这将适用于所有参数。请参阅演示
我建议您区分括号中文本的“扫描”和逗号分隔结果:
s = "something(c, d)"
s.match( /\((.+)\)/ )[1] # found text between parentheses
.split(/,/) # split the result by comma
.map(&:strip) # stripped the values
据我理解,它更像红宝石。希望能有帮助
UPD谢谢@theTinMan,有两种方法可以改进答案。首先,s[/\(.+)\/,1]
看起来比s.match(/\(.+)\)/)[1]
更好,执行速度更快。其次,按字符串拆分比按regexp拆分快。总结如下:
s = "something(c, d)"
s[ /\((.+)\)/, 1 ] # found text between parentheses
.split(',') # split the result by comma
.map(&:strip) # stripped the values
证明:
require 'benchmark'
n = 1_000_000
s = "something(c, d)"
Benchmark.bm do |x|
x.report { n.times { s.match( /\((.+)\)/ )[1].split(/,/).map(&:strip) } }
x.report { n.times { s.match( /\((.+)\)/ )[1].split(',').map(&:strip) } }
x.report { n.times { s[/\((.+)\)/, 1].split(/,/).map(&:strip) } }
x.report { n.times { s[/\((.+)\)/, 1].split(',').map(&:strip) } }
end
# user system total real
# 3.590000 0.000000 3.590000 ( 3.598151)
# 3.030000 0.000000 3.030000 ( 3.028137)
# 2.940000 0.000000 2.940000 ( 2.942490)
# 2.180000 0.000000 2.180000 ( 2.182447)
我建议您区分括号和逗号分隔结果之间的文本“扫描”:
s = "something(c, d)"
s.match( /\((.+)\)/ )[1] # found text between parentheses
.split(/,/) # split the result by comma
.map(&:strip) # stripped the values
据我理解,它更像红宝石。希望能有帮助
UPD谢谢@theTinMan,有两种方法可以改进答案。首先,s[/\(.+)\/,1]
看起来比s.match(/\(.+)\)/)[1]
更好,执行速度更快。其次,按字符串拆分比按regexp拆分快。总结如下:
s = "something(c, d)"
s[ /\((.+)\)/, 1 ] # found text between parentheses
.split(',') # split the result by comma
.map(&:strip) # stripped the values
证明:
require 'benchmark'
n = 1_000_000
s = "something(c, d)"
Benchmark.bm do |x|
x.report { n.times { s.match( /\((.+)\)/ )[1].split(/,/).map(&:strip) } }
x.report { n.times { s.match( /\((.+)\)/ )[1].split(',').map(&:strip) } }
x.report { n.times { s[/\((.+)\)/, 1].split(/,/).map(&:strip) } }
x.report { n.times { s[/\((.+)\)/, 1].split(',').map(&:strip) } }
end
# user system total real
# 3.590000 0.000000 3.590000 ( 3.598151)
# 3.030000 0.000000 3.030000 ( 3.028137)
# 2.940000 0.000000 2.940000 ( 2.942490)
# 2.180000 0.000000 2.180000 ( 2.182447)
我建议使用
s[/\(.+)\)/,1].split(',').map(&:strip)
。在我的测试中,它的速度快了2倍多。谢谢你的提示,它看起来更可读。我用所有四个变量和基准更新了答案。很好的基准。看到结果并了解什么是最快的做事方式总是很有趣的。另外,请注意在基准测试中使用Fruity,因为它为我们处理了大量的脏活。我建议使用s[/\(.+)\/,1].split(',').map(&:strip)
。在我的测试中,它的速度快了2倍多。谢谢你的提示,它看起来更可读。我用所有四个变量和基准更新了答案。很好的基准。看到结果并了解什么是最快的做事方式总是很有趣的。另外,考虑一下使用Fruity作为基准测试,因为它为我们处理了大量的脏活。