我有一組關鍵字。任何關鍵字都可以包含空格符號['one', 'one two']
。我從這些kyewords生成一個正則表達式,如/\b(?i:one|one\ two|three)\b/
。下面完整的例子:正則表達式只返回一個匹配
keywords = ['one', 'one two', 'three']
re = /\b(?i:#{ Regexp.union(keywords).source })\b/
text = 'Some word one and one two other word'
text.downcase.scan(re)
這段代碼的結果是
=> ["one", "one"]
如何找到第二個關鍵字one two
的比賽,並得到結果也是這樣嗎?
=> ["one", "one two"]
更改從最長到最短的變化順序。 – revo