获取任何长度的所有重叠匹配

时间:2018-01-23 15:40:18

标签: ruby regex match overlapping-matches

我试图实现:

df[, sprintf("mid_o_lag_%0d", 1:20) := shift(mid_o, c(1:20), type = 'lag')]
df[, sprintf("mid_d_lag_%0d", 1:20) := shift(mid_d, c(1:20), type = 'lag')]
df[, sprintf("mid_o_lead_%0d", 1:20) := shift(mid_o, c(1:20), type = 'lead')]
df[, sprintf("mid_d_lead_%0d", 1:20) := shift(mid_d, c(1:20), type = 'lead')]

可以这样做:

'abc'.scan(regex) #=> ['a', 'b', 'c', 'ab', 'bc', 'abc']

但我想用一个正则表达式来表达。

1 个答案:

答案 0 :(得分:3)

没有正则表达式怎么样?:

string = 'abc'
p (1..string.size).flat_map { |e| string.chars.each_cons(e).map(&:join) }
# ["a", "b", "c", "ab", "bc", "abc"]