【发布时间】:2015-10-19 00:07:19
【问题描述】:
def get_hashtags(post)
tags = []
post.scan(/(?<![0-9a-zA-Z])(#+)([a-zA-Z]+)/){|x,y| tags << y}
tags
end
Test.assert_equals(get_hashtags("two hashs##in middle of word#"), [])
#Expected: [], instead got: ["in"]
不应该向后看,看看匹配不是以单词或数字开头的吗?为什么它仍然接受 'in' 作为有效匹配?
【问题讨论】:
-
因为模式在第二个 # 上成功(前面没有
[0-9a-zA-Z])。
标签: ruby regex negative-lookbehind