Ruby-on-rails 如何确定在rails中是否有匹配返回true或false?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7365492/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to determine if there is a match an return true or false in rails?
提问by AnApprentice
I want to create a test that returns either true or false for email handling.
我想创建一个测试,为电子邮件处理返回 true 或 false。
For now, if the email address starts with r+ then it's true otherwise it's false. This will help our server ignore a lot of the SPAM we are getting hit with.
目前,如果电子邮件地址以 r+ 开头,则为真,否则为假。这将帮助我们的服务器忽略我们遇到的大量垃圾邮件。
Examples:
例子:
[email protected] .. true
[email protected] .. true
[email protected] .. FALSE
What's the most efficient way to handle this with Rails/ruby/regex?
使用 Rails/ruby/regex 处理这个问题的最有效方法是什么?
Thanks
谢谢
GOAL
目标
Is a one liner in rails/ruby with:
是导轨/红宝石中的单衬板,具有:
ABORT if XXXXX == 0
回答by Michael Kohl
This will match:
这将匹配:
/^r\+.*@site.com$/
Examples:
例子:
>> '[email protected]' =~ /^r\+.*@site.com$/ #=> 0
>> '[email protected]' =~ /^r\+.*@site.com$/ #=> nil
Since everything that isn't nilor falseis truthy in Ruby, you can use this regex in a condition. If you really want a boolean you can use the !!idiom:
由于在 Ruby 中所有不是nil或false不是真的,您可以在条件中使用此正则表达式。如果你真的想要一个布尔值,你可以使用!!成语:
>> !!('[email protected]' =~ /^r\+.*@site.com$/) #=> false
>> !!('[email protected]' =~ /^r\+.*@site.com$/) #=> true
回答by Kyle
If you're in Rails, there's a starts_with? method on strings:
如果你在 Rails 中,有一个 starts_with? 字符串的方法:
"foo".starts_with?('f') # => true
"foo".starts_with?('g') # => false
Outside of Rails, regexes are a reasonable solution:
在 Rails 之外,正则表达式是一个合理的解决方案:
"foo" =~ /^f/ # => true
"foo" =~ /^g/ # => false
Because Ruby uses truthiness in if statements, if you do end up using regexes, you can just use the return value to switch:
因为 Ruby 在 if 语句中使用真实性,如果你最终使用了正则表达式,你可以只使用返回值来切换:
if "foo" =~ /^f/
puts "Was true!"
else
puts "Was false!"
end
If you're writing a method and want to return a boolean result, you could always use the double bang trick:
如果你正在编写一个方法并想要返回一个布尔结果,你总是可以使用双爆技巧:
def valid_email?
!!("foo" =~ /^f/)
end
Rubular (rubular.com) is a good site for testing Ruby regexes pre-1.9. (1.9's regexes added things like lookahead.)
Rubular (rubular.com) 是测试 Ruby 1.9 之前的正则表达式的好网站。(1.9 的正则表达式添加了前瞻之类的东西。)
回答by Evmorov
If you don't want to use a "!!" operator:
如果您不想使用“!!” 操作员:
!!("foo" =~ /^f/)
you could use a ternary operator (might looks more obvious):
您可以使用三元运算符(可能看起来更明显):
"foo" =~ /^f/ ? true : false
回答by Pascal
You can use the '===' operatoras well
您可以使用“===”操作符,以及
/f/ === 'foo' #=> true
/f/ === 'bat' #=> false
Note: The regex part is on the left:
注意:正则表达式部分在左侧:
/YOUR_REGEX/ === 'YOUR_STRING'
/YOUR_REGEX/ === 'YOUR_STRING'
回答by Pete
Regexp#match?was added to Ruby-2.4.0. See Regexp#match documentation.
Regexp#match?被添加到Ruby-2.4.0。请参阅Regexp#match 文档。
irb(main):001:0> "ack!".match? /a(b|c)/
=> true
irb(main):002:0> "add".match? /a(b|c)/
=> false

