开发者

Ruby Regexp Match String Exactly

开发者 https://www.devze.com 2023-01-11 05:55 出处:网络
I\'ve a Ruby regexp question. if string == /(^\\d{1,3})/ # this matches both \"24\" and \"24 gravida ut aliquam\"

I've a Ruby regexp question.

if string == /(^\d{1,3})/ # this matches both "24" and "24 gravida ut aliquam"
  # c开发者_Python百科ode...
end

I want the regexp to match only "24".

How should I do to only allow digits?


if string =~ /(^\d{1,3}$)/
  # code...
end

Incidentally, if you only want to match "24" (not "39" or "42") you don't want a regex, you want to do a direct comparison:

if string == "24"
  # code...
end
0

精彩评论

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