wood burning stoves 2.0*
The moose likes JDBC and the fly likes Escape Special Character in Mysql Regexp Search Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Escape Special Character in Mysql Regexp Search" Watch "Escape Special Character in Mysql Regexp Search" New topic
Author

Escape Special Character in Mysql Regexp Search

shreya vinay
Greenhorn

Joined: Sep 02, 2005
Posts: 6
Hi,

I need to search for exact match of c++, so escaped it with backslash
and used regexp wordboundries like this

SELECT * FROM tablename j where j.fieldname REGEXP '[[:<:]](C\\+\\+)[:>:]]'
It is not displaying any result. But if i search with this syntax
SELECT * FROM tablename j where j.fieldname REGEXP 'C\\+\\+'
It displays all results other than exact match like VC++ which i dont need.

I guess problem with escape ++ and regexp exact match. Please suggest me a solution for this.

Thanks in advance,
Shreya.
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5812
    
    7

How about:

SELECT * FROM tablename j where j.fieldname REGEXP '^C\\+\\+$'

The '^' matches start-of-expression, and '$' matches end-of-expression.


JBoss In Action
shreya vinay
Greenhorn

Joined: Sep 02, 2005
Posts: 6
Hi,

Thank you so much for the query. It works well for c++ but it is not working for some words like .net, c#. i need exact match for this also.can you please suggest a solution for these words?

Best Regards,
Shreya.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38007
    
  22
The chances of a regex for .c++ matching .net are not very good. Suggest you write what you think would match, please.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Escape Special Character in Mysql Regexp Search
 
Similar Threads
java sql query
Taking entries from database containing %
Can PreparedStatements take care of this ?
How to do a SELECT WHERE a LIKE b when b has a '%'?
Problem with escape chars...