使用Ruby Regular Expression匹配字符串的所有单词排列

时间:2012-01-23 13:55:57

标签: ruby regex

我有一连串的话:

string = "Ruby web framework"

如何编写匹配所有可能的单词排列的正则表达式?

以下所有字符串必须匹配:

ruby web framework
web framework ruby
framework ruby web

ruby framework web
framework web ruby
web ruby framework 

2 个答案:

答案 0 :(得分:4)

要指出使用正则表达式并不是实现这一目标的最快途径:

pp string.split.permutation.to_a

[["Ruby", "web", "framework"],
 ["Ruby", "framework", "web"],
 ["web", "Ruby", "framework"],
 ["web", "framework", "Ruby"],
 ["framework", "Ruby", "web"],
 ["framework", "web", "Ruby"]]

叹息......好的......这就是我的表现:

require 'pp'

string = "Ruby web framework"
strings_to_search = string.split.permutation.map{ |p| 'Lorem, ' + p.join(' ') + ' consectetur'}

strings_to_search << 'Lorem, Ruby consectetur' 
strings_to_search << 'Lorem, Ruby web consectetur' 
strings_to_search << 'Lorem, Ruby Ruby Ruby' 

pp strings_to_search.map{ |p| p.scan(Regexp.union(string.split)) }

发生了什么:

1.9.2-p290 :001 >     require 'pp'
true
1.9.2-p290 :002 > 
1.9.2-p290 :003 >       string = "Ruby web framework"
"Ruby web framework"
1.9.2-p290 :004 >     strings_to_search = string.split.permutation.map{ |p| 'Lorem, ' + p.join(' ') + ' consectetur'}
[
    [0] "Lorem, Ruby web framework consectetur",
    [1] "Lorem, Ruby framework web consectetur",
    [2] "Lorem, web Ruby framework consectetur",
    [3] "Lorem, web framework Ruby consectetur",
    [4] "Lorem, framework Ruby web consectetur",
    [5] "Lorem, framework web Ruby consectetur"
]
1.9.2-p290 :005 > 
1.9.2-p290 :006 >       strings_to_search << 'Lorem, Ruby consectetur' 
[
    [0] "Lorem, Ruby web framework consectetur",
    [1] "Lorem, Ruby framework web consectetur",
    [2] "Lorem, web Ruby framework consectetur",
    [3] "Lorem, web framework Ruby consectetur",
    [4] "Lorem, framework Ruby web consectetur",
    [5] "Lorem, framework web Ruby consectetur",
    [6] "Lorem, Ruby consectetur"
]
1.9.2-p290 :007 >     strings_to_search << 'Lorem, Ruby web consectetur' 
[
    [0] "Lorem, Ruby web framework consectetur",
    [1] "Lorem, Ruby framework web consectetur",
    [2] "Lorem, web Ruby framework consectetur",
    [3] "Lorem, web framework Ruby consectetur",
    [4] "Lorem, framework Ruby web consectetur",
    [5] "Lorem, framework web Ruby consectetur",
    [6] "Lorem, Ruby consectetur",
    [7] "Lorem, Ruby web consectetur"
]
1.9.2-p290 :008 >     strings_to_search << 'Lorem, Ruby Ruby Ruby' 
[
    [0] "Lorem, Ruby web framework consectetur",
    [1] "Lorem, Ruby framework web consectetur",
    [2] "Lorem, web Ruby framework consectetur",
    [3] "Lorem, web framework Ruby consectetur",
    [4] "Lorem, framework Ruby web consectetur",
    [5] "Lorem, framework web Ruby consectetur",
    [6] "Lorem, Ruby consectetur",
    [7] "Lorem, Ruby web consectetur",
    [8] "Lorem, Ruby Ruby Ruby"
]
1.9.2-p290 :009 > 
1.9.2-p290 :010 >       pp strings_to_search.map{ |p| p.scan(Regexp.union(string.split)) }
[["Ruby", "web", "framework"],
 ["Ruby", "framework", "web"],
 ["web", "Ruby", "framework"],
 ["web", "framework", "Ruby"],
 ["framework", "Ruby", "web"],
 ["framework", "web", "Ruby"],
 ["Ruby"],
 ["Ruby", "web"],
 ["Ruby", "Ruby", "Ruby"]]
[
    [0] [
        [0] "Ruby",
        [1] "web",
        [2] "framework"
    ],
    [1] [
        [0] "Ruby",
        [1] "framework",
        [2] "web"
    ],
    [2] [
        [0] "web",
        [1] "Ruby",
        [2] "framework"
    ],
    [3] [
        [0] "web",
        [1] "framework",
        [2] "Ruby"
    ],
    [4] [
        [0] "framework",
        [1] "Ruby",
        [2] "web"
    ],
    [5] [
        [0] "framework",
        [1] "web",
        [2] "Ruby"
    ],
    [6] [
        [0] "Ruby"
    ],
    [7] [
        [0] "Ruby",
        [1] "web"
    ],
    [8] [
        [0] "Ruby",
        [1] "Ruby",
        [2] "Ruby"
    ]
]

点击后, THEN 会过滤掉那些不应被接受的内容。不要试图在正则表达式中完成所有操作,因为你会使一个模式变得太笨重并且是一个维护噩梦。

答案 1 :(得分:1)

这有效:

   theRegex=Regexp.new("("+(Regexp.union(str.split(' ')).inspect.chop[1..-1])+"){#{str.split(' ').length}}")

生成的正则表达式为/(ruby|web|development){3}/。这是你想要的吗?