Forum Moderators: coopster
This is a question I've been searching for the answer to for a while without much success, so I thought I'd ask in case anyone here had any suggestions.
Basically, to give a simple example, I have a single user input on a page. I then have a database, containing 100 records, each with a field containing a regular expression.
What I would like to do is do a reverse comparison. Rather than using a regular expression and returning the results that match, I want to use the input from the user and return the results where the regular expression matches the input.
To give a simpler example, say the user input was
'this is a test'
and in the database there were the following records:
ID¦ RegExp
===============================
1 ¦ this (is)? a test
2 ¦ (.*)?
3 ¦ ^(([a-zA-Z]).*)?$
4 ¦ [0-9]
5 ¦ this is (one¦another) test
I would like to be able to return the rows 1,2 and 3 as the regular expression matches the input.
Is this even possible? If so, how would I go about doing this?
Would I need to loop it through PHP, or is there a special query I could use that I simply was not aware of?
Thanks a lot, I appreciate any help.
Simon
In regards to 1) - Would doing that for around 100 regexp's be a problem for the server (this would possibly be happening quite frequently)?
In regards to 2) - Yes, I had a look through there, but I could only see matching text based on a regexp, not matching a regexp based on text :( - is there some way to achieve this directly?
mysql> create table regs ( exp VARCHAR(10) );
mysql> insert into regs (exp) values ('a+b*');
then did the following select statement:
mysql>select * from regs where 'aabb' REGEXP exp;
this returns the row from the table where the value of exp matches as a regular expression against the string 'aabb'
Hope this helps.
How much of a load depends on the regex and the test string. If the text snippets are short and the regexes don't have a lot of backtracking, it will be fast. It is possible to have single regexes that will be very slow though. You should build it, try it, and if it seems slow, figure out where the bottlenecks are using a profiler like xdebug and then try rewriting those regexes.