[英]Regex is not matching negative integers
I have a query for my Payment
model. 我有一个关于我的
Payment
模型的查询。 My amount
column is an integer. 我的
amount
栏是整数。 I want to eliminate negative integers from my amount
column. 我想从我的
amount
栏中消除负整数。 I just want to show payments with positive values. 我只想显示正值付款。
Payment.where('amount NOT REGEXP ?', '^-\d+$')
and 和
Payment.where('amount NOT REGEXP ?', '^-\d*\.?\d+$'
both work in Rubular, but do not work in my Rails app. 两者都可以在Rubular中工作,但不能在我的Rails应用程序中工作。 I tried matching in the console, and it does not work too:
我尝试在控制台中进行匹配,但也无法正常工作:
amount.to_i =~ /^-\d*\.?\d+$/
I know that >= 0
condition will work, but I wonder why it does not work with REGEX
. 我知道
>= 0
条件会起作用,但是我想知道为什么它不适用于REGEX
。
如果您只是想删除负整数,那么在提高性能的同时,这种方法是否也能正常工作?
Payment.where('amount >= 0')
I think your regexp works in console. 我认为您的正则表达式可以在控制台中使用。
$ irb
irb(main):001:0> /^-\d*\.?\d+$/ =~ "-3"
=> 0
irb(main):002:0> $~
=> #<MatchData "-3">
irb(main):003:0> /^-\d*\.?\d+$/ =~ "-3.1415"
=> 0
irb(main):004:0> $~
=> #<MatchData "-3.1415">
irb(main):005:0> /^-\d*\.?\d+$/ =~ "128"
=> nil
irb(main):006:0>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.