用于返回所有可能的顺序单词组合的正则表达式


Regex for returning all possible sequential word groupings

对于给定的字符串"the fox jump over the rabbit",下面的字符串被认为是顺序的单词分组

狐狸跳过兔子,
狐狸跳过了,
狐狸跳了过来,
狐狸跳了起来,
狐狸,
狐狸跳过兔子,
小狐跳了过去,
狐狸跳了过来,
狐狸跳,
跳过兔子,

跳过,
在兔子身上,
在,
兔子

谁能建议或提供一个合适的正则表达式。我尝试了几种

的变体
'b'w*'b's+('b'w*+'b's?

,但我似乎不能得到一个表达式,返回完整的预期结果集。

问候,SOliver .

Codepad: http://codepad.org/E4rywXD8

$s = "the fox jumped over the rabbit";
$s = split(' ', $s);
$result = array();
foreach ($s as $key => $word)
{
    $r = array();
    for ($i = $key; $i < count($s); $i++)
    {
        $r[] = $s[$i];
        if(count($r) > 1) $result[] = implode(' ', $r);
    }
}

下面是一个使用标准in/out的正则表达式的Ruby脚本:

@map = {}
def scan(str)
    if(str && str=~/'w+'s'w+/)
        @map[str] = nil
        scan(str.sub(/'s?'w+$/,""))
        scan(str.sub(/^'w+'s?/,""))
    end
end
scan(gets)
puts @map