我有一个满是数字的表格,需要验证每个数字是否有0位小数并且分隔符'''用于'000s。
赞:123,456
我该如何验证?
由于
答案 0 :(得分:1)
s = '74,456,890'
if s =~ /^\d{1,3}(?:,\d{3}+)$/
# String s has the required format
else
# String s does not have the required format
end
答案 1 :(得分:0)
假设数字是叮咬(因为有,
符号):
numbers = [
'100,000',
'100,246.40',
'100,000,304',
'300.210',
'10,200'
]
numbers.map { |n| n =~ /^\d{1,3},((\d{3})|(\d{3},))+$/ ? n : nil }.compact
#=> ["100,000", "100,000,304", "10,200"]