我正在尝试获取扫描结果的索引(偏移),我知道扫描以纯字符串(?)返回值。在第二个示例中,我看到仍然有可用的偏移量,但是我无法弄清楚如何正确使用它,是否有可能使它在单行中工作(我有循环选项)。理想情况下,对于我的结果,我需要所有索引的逗号分隔值。
# trying to get results as: 7,19,32
lineRegex = 'TableA'
line = 'select tableA from tableAxx and tableA '
regex = /#{lineRegex}/ix
line.scan(regex) ##### => ["tableA", "tableA", "tableA"]
# [$~.offset(0)[0]] ### [32] last offset only
# this works but Id like without loop
res = []
regex = /#{lineRegex}/ix
line.scan(regex) do |c|
res << [$~.offset(0)[0]]
end
puts res
#res.inspect ####### => "[[7], [19], [32]]"