tags:

views:

188

answers:

2

I have a very interesting task, which I don't know how to implement.

I need to store many regular expressions in a database table, and need to be able to find which of them matches the given string.

For example:

id | regexp
---|-------------
1  | ^hello world$
2  | ^I have [0-9] flowers&
3  | ^some other regexp$ 
4  | ^and another (one|regexp)$

And I need to find which of those expressions matches string "I have 5 flowers". Of course I can SELECT * FROM table and loop through an expressions matching them one by one in PHP, but this would be horrible for server to handle.

Can I somehow index this table or use a special SQL query to handle this task?

I'll appreciate any answer. Thank you.

+5  A: 
select * from table where $your_string RLIKE regexp 

mysql regular expressions

stereofrog
Thank you, I used RLIKE before, but didn't think of switching arguments :)
Silver Light
What kind of index should I use, to fit best this situation? Just index the regexp column?
Silver Light
hmm, i don't think RLIKE uses indexes.
stereofrog
You learn something new everyday :)
Gipsy King
A: 
SELECT * FROM table WHERE 'some stuff' REGEXP `regexp`;

Unfortunately there is no way to use indexes with queries that use regexps.

Romuald Brunet