量词和后视问题

### Ruby 1.8.7 ### require 'rubygems' require 'oniguruma' # for look-behind Oniguruma::ORegexp.new('h(?=\w*)') # => /h(?=\w*)/ Oniguruma::ORegexp.new('(? ArgumentError: Oniguruma Error: invalid pattern in look-behind Oniguruma::ORegexp.new('(? /(? # "hello".match(/(? SyntaxError: (irb):3: invalid pattern in look-behind: /(?<=\w*)o/ "hello".match(/(? # 

我不能使用后视量词吗?

问题是Ruby不支持可变长度的lookbehinds。 量词不是本身,但它们不能使后视的长度不确定。

Perl具有相同的限制,几乎所有主要语言都具有正则表达式。

尝试使用直接匹配(\w*)\W*?o而不是lookbehind。

我正在敲打同样的问题,Borealid的回答帮助解释了这个问题。

然而,这让我思考。 也许量词不需要在lookbehind 内部 ,但可以应用于lookbehind 本身?

 "hello".match(/(?<=\w*)o/) # => SyntaxError: (irb):3: invalid pattern in look-behind: /(?<=\w*)o/ "hello".match(/(?<=\w)*o/) # => # 

所以现在我们有一个可变数量的恒定长度的lookbehinds。 似乎为我绕过了这个问题。 🙂