Forum Moderators: coopster

Message Too Old, No Replies

Reverse regexp comparison?

Going about this the other way

         

BlueScreen

1:19 pm on Jan 22, 2006 (gmt 0)

10+ Year Member



Hello,

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.

simon2263

1:58 pm on Jan 22, 2006 (gmt 0)

10+ Year Member



I see two options: either
(1) fetch all the records from the database and match them successively against the user input, noting successful matches, or
(2) use regular expressions in the MySQL query (see [dev.mysql.com...] and the discussion of the REGEXP operator).

Simon

BlueScreen

2:26 pm on Jan 22, 2006 (gmt 0)

10+ Year Member



Thanks for the reply.

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?

simon2263

7:41 pm on Jan 22, 2006 (gmt 0)

10+ Year Member



If the number of regexps is only 100 then it should be okay. As to option (2), as a test, I set up the following table in mysql:

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.

ergophobe

8:33 pm on Jan 22, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Several of the preg_* functions take arrays as arguments, so you could load 100 regexes into the array and then run it.

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.