Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement a SQL like 'LIKE' operator in java?

I need a comparator in java which has the same semantics as the sql 'like' operator. For example:

myComparator.like("digital","%ital%"); myComparator.like("digital","%gi?a%"); myComparator.like("digital","digi%"); 

should evaluate to true, and

myComparator.like("digital","%cam%"); myComparator.like("digital","tal%"); 

should evaluate to false. Any ideas how to implement such a comparator or does anyone know an implementation with the same semantics? Can this be done using a regular expression?

like image 727
Chris Avatar asked May 22 '09 15:05

Chris


People also ask

How do you write a like query in Java?

String sql = "SELECT * FROM drawings WHERE name LIKE ?"; // ... preparedStatement = connection. prepareStatement(sql); preparedStatement. setString(1, "%" + DT + "%"); resultSet = preparedStatement.

What is like %% in SQL?

The LIKE operator is used in a WHERE clause to search for a specified pattern in a column. There are two wildcards often used in conjunction with the LIKE operator: The percent sign (%) represents zero, one, or multiple characters. The underscore sign (_) represents one, single character.

Can we use in operator with like in SQL?

the LIKE operation is not permitted to be used with IN.


2 Answers

.* will match any characters in regular expressions

I think the java syntax would be

"digital".matches(".*ital.*"); 

And for the single character match just use a single dot.

"digital".matches(".*gi.a.*"); 

And to match an actual dot, escape it as slash dot

\. 
like image 156
Bob Avatar answered Sep 24 '22 07:09

Bob


Yes, this could be done with a regular expression. Keep in mind that Java's regular expressions have different syntax from SQL's "like". Instead of "%", you would have ".*", and instead of "?", you would have ".".

What makes it somewhat tricky is that you would also have to escape any characters that Java treats as special. Since you're trying to make this analogous to SQL, I'm guessing that ^$[]{}\ shouldn't appear in the regex string. But you will have to replace "." with "\\." before doing any other replacements. (Edit: Pattern.quote(String) escapes everything by surrounding the string with "\Q" and "\E", which will cause everything in the expression to be treated as a literal (no wildcards at all). So you definitely don't want to use it.)

Furthermore, as Dave Webb says, you also need to ignore case.

With that in mind, here's a sample of what it might look like:

public static boolean like(String str, String expr) {     expr = expr.toLowerCase(); // ignoring locale for now     expr = expr.replace(".", "\\."); // "\\" is escaped to "\" (thanks, Alan M)     // ... escape any other potentially problematic characters here     expr = expr.replace("?", ".");     expr = expr.replace("%", ".*");     str = str.toLowerCase();     return str.matches(expr); } 
like image 25
Michael Myers Avatar answered Sep 26 '22 07:09

Michael Myers