开发者

How do you return a boolean value for a regexp scan of an integer?

开发者 https://www.devze.com 2023-04-12 01:08 出处:网络
I\'m looking through my object attributes开发者_JS百科 for culprits that are not : ^[1-3]{3}$ What is the method used to scan integers for regexp?Some examples:

I'm looking through my object attributes开发者_JS百科 for culprits that are not :

^[1-3]{3}$

What is the method used to scan integers for regexp?


Some examples:

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

Since nil is considered as false, you have your boolean.

Ex:

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


You may use also one of the following:

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

or

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号