你能在rails中找到带正则表达式的地方吗?

时间:2022-07-14 23:23:58

I'm trying to find objects that begin with a number.

我正在尝试找到以数字开头的对象。

Syntactically this is off. But I would like to do something like this :

从句法上说这是关闭的。但我想做这样的事情:

Object.where([name LIKE ?', /[1-9]/])

If that isn't possible, how do you suppose the best way to find all objects that start with a number?

如果那是不可能的,那么您如何设想找到以数字开头的所有对象的最佳方法?

2 个解决方案

#1


1  

Select values that begin with a number

选择以数字开头的值

Object.where(['name REGEXP ?', '^[0-9]'])

#2


2  

You can use rlike / regexp i would think. Though it's not portable across to other databases.

您可以使用rlike / regexp。虽然它不可移植到其他数据库。

http://dev.mysql.com/doc/refman/5.0/en/regexp.html#operator_regexp

http://dev.mysql.com/doc/refman/5.0/en/regexp.html#operator_regexp

Object.where(['name rlike ?', '^[\d]'])

#1


1  

Select values that begin with a number

选择以数字开头的值

Object.where(['name REGEXP ?', '^[0-9]'])

#2


2  

You can use rlike / regexp i would think. Though it's not portable across to other databases.

您可以使用rlike / regexp。虽然它不可移植到其他数据库。

http://dev.mysql.com/doc/refman/5.0/en/regexp.html#operator_regexp

http://dev.mysql.com/doc/refman/5.0/en/regexp.html#operator_regexp

Object.where(['name rlike ?', '^[\d]'])