WizLiz
WizLiz

Reputation: 2098

Check if two Strings contain the same pattern

my application is going to need a string matching function between an inputed string and a string stored in my DB. Those strings concerns product dimensions (like length, heigh etc.) My goal is to check if the string inputed already exists in my database but maybe with another pattern , for example :

input : "vis 4*40" should return true if I have something like "vis 4X40" or "vis 4 x 40" or "vis 4* 40" in my database.

The only way i've found yet is to normalize my databse using a regexp to replace pattern like :

 \d+ *[xX*] *\d+

with a normalized one (for example NumberXNumber), do the same with the string in input then try to find it. However I was wondering if any tool already existed and would allow me to do that. (I'm working in C# .NET V4)

Upvotes: 1

Views: 321

Answers (2)

Anirudha
Anirudha

Reputation: 32797

A better way would be to concatenate both the data and then match them

string target=inputString+"#"+storedString;

if(Regex.IsMatch(target,"(\d+) *[xX*] *(\d+)#\1 *([xX*]) *\2"))
        //the string is in the database
else
       //it is not

Upvotes: 1

hometoast
hometoast

Reputation: 11782

Just use the user-provided wildcard, replace the * with % and query your table: SELECT col FROM table WHERE col LIKE 'vis 4%40'

Please make sure you execute your queries with parameters.

Upvotes: 0

Related Questions