sabrams
sabrams

Reputation: 1128

Rails SQL regular expression

I'm trying to search for the maximum number in the series A0001, A0002, A1234, A2351, etc... The problem is that the list I'm searching in also has strings such as AG108939, E092357, AL399, 22-30597, etc...

So basically, I want the Highest A#### value in my database. I was using the following query:

@max_draw = Drawing.where("drawing_number LIKE ?", "A%")

Which was working until numbers such as AG309 started getting in the way because it starts with an A, but has a different format than what I'm looking for.

I'm assuming this should be pretty straight forward with regular expressions, but I'm new to this and don't know how to correctly write this query with a regular expression. Here are some things I've tried that just return nil:

 @max_draw = Drawing.where("drawing_number LIKE ?", /A\d+/)
 @max_draw = Drawing.where("drawing_number LIKE ?", "/A\d+/")
 @max_draw = Drawing.where("drawing_number LIKE ?", "A[0-9]%")

Upvotes: 49

Views: 40504

Answers (4)

An alternative solution for this is using the Arel::Predications method matches_regexp.

First of all, you create an Arel table.

users = User.arel_table

And then you perform the query with a regexp

User.where(users[:email].matches_regexp('(.*)\@gmail.com'))

By default, this method does a case sensitive query. If you need to do a case insensitive query, you need to add case_sensitive = false as an option.

Example,

User.where(users[:email].matches_regexp('(.*)\@gmail.com', case_sensitive = false))

This method supports PostgreSQL and MySQL

More info: https://github.com/rails/rails/pull/36800

Upvotes: 1

Epigene
Epigene

Reputation: 3918

On Rails 4+ with a Postgres database the general form of a RegEx query is:

Model.where("column ~* ?", 'regex')

As for the regex, it can be a general '^A\d+$' or more specific '^A\d{4}$' Breaking it down:

^ - string start anchor
A - literal "A"
\d+ - one or more digits (0-9)
\d{4} - exactly four digits
$ - string end anchor

Basically, the regex reads "the string should start with an A, followed by four digits and then the string should end". The final query line is:

@max_draw = Drawing.where("drawing_number ~* ?", '^A\d{4}$')

Further reading on ruby RegEx at RubyDoc or the more accessible Perl variant (used by Sublime text)

Upvotes: 87

davegson
davegson

Reputation: 8331

You did a good job! The thing missing was the REGEXP function which is used for regex in queries:

So in your case use

Drawing.where("drawing_number REGEXP ?", 'A\d{4}')
# the {4} defines that there have to be exactly 4 numbers, change if you need to

In SQL you use the '-colons, which is weird because you normally start regex with /-backslashes

Upvotes: 34

Andrew Kothmann
Andrew Kothmann

Reputation: 607

You can't use regular expressions in SQL which is what you're trying to do. Your best bet would be to select just the entries that start with A like your original code, then skip entries that have more than one letter at the beginning.

items = Drawing.where( [ 'drawing_number LIKE ?' , 'A%' ] )
max_value = 0
items.each do |item|
  next if item.drawing_number =~ /\A[A-Za-z]{2,}/
  drawing_number = item.drawing_number.gsub(/\AA/, '').to_i
  max_value = drawing_number if drawing_number > max_value
end

I'm reasonably certain it's possible to get this shorter but this should do what you need.

(\A is the start of line anchor that works with strings containing newlines)

({2,} matches two or more of the proceeding character range)

http://www.rubular.com/ is awesome for testing ruby regexes.

Upvotes: -3

Related Questions