如何为整数的正则表达式扫描返回布尔值



我正在检查我的对象属性是否有错误:

^[1-3]{3}$

用于扫描整数的regexp方法是什么?

一些例子:

124.to_s.match(/^[1-3]{3}$/)
=> nil
123.to_s.match(/^[1-3]{3}$/)
=>#<MatchData "123">

因为nil被认为是false,你有你的布尔值。

,

 "no yo" if 124.to_s.match(/^[1-3]{3}$/)
 => nil
 "yo!" if 123.to_s.match(/^[1-3]{3}$/)
 => "yo!"

您也可以使用下列方式之一:

def is_pure_integer?(i)
  i.to_i.to_s == i.to_s
end

'132' =~ /^d+$/ ? true : false

最新更新