Javascript 适合你的第一个例子。对于第二个问题,您可能是正确的。我不知道该如何适应它。这就是为什么我写这句话的原因,说这取决于OP到底想实现什么。@WilliamFengabcadcdc中的内容不应该包括abcadc,adcdc?读者:第一句话明确指出,我们需要的是一

Javascript 适合你的第一个例子。对于第二个问题,您可能是正确的。我不知道该如何适应它。这就是为什么我写这句话的原因,说这取决于OP到底想实现什么。@WilliamFengabcadcdc中的内容不应该包括abcadc,adcdc?读者:第一句话明确指出,我们需要的是一,javascript,ruby,regex,Javascript,Ruby,Regex,适合你的第一个例子。对于第二个问题,您可能是正确的。我不知道该如何适应它。这就是为什么我写这句话的原因,说这取决于OP到底想实现什么。@WilliamFengabcadcdc中的内容不应该包括abcadc,adcdc?读者:第一句话明确指出,我们需要的是一个调用所有匹配项(字符串,正则表达式),而不是,它适用于任意字符串和正则表达式。通常在正则表达式引擎中,匹配是“最左最长的”,除非您指定一个非贪婪的量词,在这里您可以得到“最左最短的”匹配。您不能期望在一个表达式中同时获得最短和最长的表达式。获


适合你的第一个例子。对于第二个问题,您可能是正确的。我不知道该如何适应它。这就是为什么我写这句话的原因,说这取决于OP到底想实现什么。@WilliamFeng
abcadcdc
中的内容不应该包括
abcadc
adcdc
?读者:第一句话明确指出,我们需要的是一个调用
所有匹配项(字符串,正则表达式)
,而不是,它适用于任意字符串和正则表达式。通常在正则表达式引擎中,匹配是“最左最长的”,除非您指定一个非贪婪的量词,在这里您可以得到“最左最短的”匹配。您不能期望在一个表达式中同时获得最短和最长的表达式。获得所有最短值,然后找到所有串联排列将是最好的策略。@theTinMan,最初的问题是关于给定正则表达式的匹配,这只是一个“让我们说”的示例。编辑之后,这个特定的正则表达式匹配看起来像是问题的要点。我不同意你的编辑。请随意编辑。这就是它的工作原理。“让我们说”是虚构的。我们正在处理编程和创建一本关于SO的参考书,所以猜测很少是合适的。请解释为什么这是有用的。建议代码很好,但解释代码的正确性可以教育那些搜索解决方案的人,以便他们以后可以重用它。将其放在答案中,而不是注释中。您的代码片段清楚地显示生成器的工作方式+1:)
def matching_substrings(string, regex)
  string.size.times.each_with_object([]) do |start_index, maching_substrings|
    start_index.upto(string.size.pred) do |end_index|
      substring = string[start_index..end_index]
      maching_substrings.push(substring) if substring =~ /^#{regex}$/
    end
  end
end

matching_substrings('abcadc', /a.*c/) # => ["abc", "abcadc", "adc"]
matching_substrings('foobarfoo', /(\w+).*\1/) 
  # => ["foobarf",
  #     "foobarfo",
  #     "foobarfoo",
  #     "oo",
  #     "oobarfo",
  #     "oobarfoo",
  #     "obarfo",
  #     "obarfoo",
  #     "oo"]
matching_substrings('why is this downvoted?', /why.*/)
  # => ["why",
  #     "why ",
  #     "why i",
  #     "why is",
  #     "why is ",
  #     "why is t",
  #     "why is th",
  #     "why is thi",
  #     "why is this",
  #     "why is this ",
  #     "why is this d",
  #     "why is this do",
  #     "why is this dow",
  #     "why is this down",
  #     "why is this downv",
  #     "why is this downvo",
  #     "why is this downvot",
  #     "why is this downvote",
  #     "why is this downvoted",
  #     "why is this downvoted?"]
▶ str = "abcadc"
▶ from = str.split(/(?=\p{L})/).map.with_index { |c, i| i if c == 'a' }.compact
▶ to   = str.split(/(?=\p{L})/).map.with_index { |c, i| i if c == 'c' }.compact
▶ from.product(to).select { |f,t| f < t }.map { |f,t| str[f..t] }
#⇒ [
#  [0] "abc",
#  [1] "abcadc",
#  [2] "adc"
# ]
str = "abcadc"
[/(a[^c]*c)/, /(a.*c)/].flat_map{ |pattern| str.scan(pattern) }.reduce(:+)
# => ["abc", "adc", "abcadc"]
def all_matches(str, regex)
  (n = str.length).times.reduce([]) do |subs, i|
     subs += [*i..n].map { |j| str[i,j-i] }
  end.uniq.grep /^#{regex}$/
end

all_matches("abcadc", /a.*c/) 
#=> ["abc", "abcadc", "adc"]
function allMatches(str, regex) {
  var i, j, len = str.length, subs={};
  var anchored = new RegExp('^' + regex.source + '$');
  for (i=0; i<len; ++i) {
    for (j=i; j<=len; ++j) {
       subs[str.slice(i,j)] = true;
    }
  }
  return Object.keys(subs).filter(function(s) { return s.match(anchored); });
}
class String
  def all_matches(regex)
    return [] if empty?
    r = /^#{regex}$/
    1.upto(size).with_object([]) { |i,a|
      a.concat(each_char.each_cons(i).map(&:join).select { |s| s =~ r }) }
  end
end

'abcadc'.all_matches /a.*c/
  # => ["abc", "abcadc", "adc"]
'aaabaaa'.all_matches(/a.*a/)
  #=> ["aa", "aa", "aa", "aa", "aaa", "aba", "aaa", "aaba", "abaa", "aaaba",
  #    "aabaa", "abaaa", "aaabaa", "aabaaa", "aaabaaa"]