【发布时间】:2013-01-16 15:13:42
【问题描述】:
我刚刚对 \B 和 \b 有了一些概念。并且accordinlgy 尝试了一个代码(取自互联网)但无法理解 - 那些regexp Anchors 是如何生成输出的。所以任何人请帮助我理解\B 和\b 之间的区别,在内部说明它们在Ruby 中的pattern matching 是如何处理的?
Interactive ruby ready.
> str = "Hit him on the head\n" +
"Hit him on the head with a 2×4\n"
=> "Hit him on the head
Hit him on the head with a 2??4
"
> str.scan(/\w+\B/)
=> ["Hi", "hi", "o", "th", "hea", "Hi", "hi", "o", "th", "hea", "wit"]
> str.scan(/\w+\b/)
=> ["Hit", "him", "on", "the", "head", "Hit", "him", "on", "the", "head", "with", "a", "2", "4"]
>
谢谢,
【问题讨论】:
-
为什么在
str的上述输出中出现2??4而不是2x4?
标签: ruby regex ruby-1.9.3