Ruby正则表达式:拒绝整个单词

Ruby正则表达式:拒绝整个单词,ruby,regex,Ruby,Regex,我知道在正则表达式中,可以拒绝符号列表,例如[^abc]。我希望在我的输入中看到一个完整的词。 更准确地说,我想拒绝“打印”。 举几个例子: print all - match frokenfooster - no match print all nomnom - no match print bollocks - no match print allpies - no match 你在找一份工作。(参考) 将取消模式中“排除”一词的资格。正则表达式支持分词\b 搜索字符串中是否存在单词“al

我知道在正则表达式中,可以拒绝符号列表,例如
[^abc]
。我希望在我的输入中看到一个完整的词。 更准确地说,我想拒绝“打印”。 举几个例子:

print all - match
frokenfooster - no match
print all nomnom - no match
print bollocks - no match
print allpies - no match
你在找一份工作。(参考)


将取消模式中“排除”一词的资格。

正则表达式支持分词
\b

搜索字符串中是否存在单词“all”非常简单:

>> 'the word "all"'[/\ball\b/] #=> "all"
>> 'the word "ball"'[/\ball\b/] #=> nil
>> 'all of the words'[/\ball\b/] #=> "all"
>> 'we had a ball'[/\ball\b/] #=> nil
>> 'not ball but all'[/\ball\b/] #=> "all"

请注意,将其锚定到字符串的开头或结尾并不需要,因为
\b
也将字符串的开头和结尾识别为单词边界。

我觉得“我想匹配”,而您提供的示例是矛盾的?@Mike:p.s.我想您正在寻找
^print((?!all$).$
;-)错过了锚。现在似乎是防弹的了如果它在一条直线的中间,它也不会取消“排除”吗?@是的,这就是为什么你必须封装它。我不知道,这真的很方便。
>> 'the word "all"'[/\ball\b/] #=> "all"
>> 'the word "ball"'[/\ball\b/] #=> nil
>> 'all of the words'[/\ball\b/] #=> "all"
>> 'we had a ball'[/\ball\b/] #=> nil
>> 'not ball but all'[/\ball\b/] #=> "all"