Frank
Frank

Reputation: 1864

escaping hashtags in regular expressions in sql

I'm using an sql query to search my table of posts for hashtags then a word. E.g. #cats I'm using regexp because it enables me to use a regular expression to find something in the string matching #cats but with a pattern.

The problem is I think the # is breaking the query. Here's my query,

mysql_query("SELECT * FROM somewhere WHERE something regexp '[[:<:]]#cats[[:>:]]'");

I've tried this to escape the #

mysql_query("SELECT * FROM somewhere WHERE something regexp '[[:<:]]\\#\cats[[:>:]]'");

Could anybody help me escape this hash, it is annoying me to so much :(

Upvotes: 1

Views: 1315

Answers (1)

Mark Byers
Mark Byers

Reputation: 838376

The problem is that there is no word boundary between a space and #.

Try this instead:

WHERE something REGEXP '#cats[[:>:]]'

Upvotes: 3

Related Questions