0

I have about 100,000 strings in database and I want to if there is a way to automatically generate regex pattern from these strings. All of them are alphabetic strings and use set of alphabets from English letters. (X,W,V) is not used for example. Is there any function or library that can help me achieve this target in C#? Example strings are

KHTK
RAZ

Given these two strings my target is to generate a regex that allows patterns like (k, kh, kht,khtk, r, ra, raz) case insensitive of course. I have downloaded and used some C# applications that help in generating regex but that is not useful in my scenario because I want a process in which I sequentially read strings from db and add rules to regex so this regex could be reused later in the application or saved on the disk.

I'm new to regex patterns and don't know if the thing I'm asking is even possible or not. If it is not possible please suggest me some alternate approach.

Anderson Green
  • 30,230
  • 67
  • 195
  • 328
Muhammad Adeel Zahid
  • 17,474
  • 14
  • 90
  • 155
  • This is not an answer to your question, but since you mention you're new to RegEx I would like to point out a great resource I recently came across for learning about and testing RegEx's http://gskinner.com/RegExr/ – Eric J. May 28 '10 at 17:02

1 Answers1

2

A simple (some might say naive) approach would be to create a regex pattern that concatenates all the search strings, separated by the alternation operator |:

  1. For your example strings, that would get you KHTK|RAZ.
  2. To have the regex capture prefixes, we would include those prefixes in the pattern, e.g. K|KH|KHT|KHTK|R|RA|RAZ.
  3. Finally, to make sure that those strings are captured only in whole, and not as part of larger strings, we'll match the beginning-of-line and end-of-line operators and the beginning and end of each string, respectively: ^K$|^KH$|^KHT$|^KHTK$|^R$|^RA$|^RAZ$

We would expect the Regex class implementation to do the heavy lifting of converting the long regex pattern string to an efficient matcher.

The sample program here generates 10,000 random strings, and a regular expression that matches exactly those strings and all their prefixes. The program then verifies that the regex indeed matches just those strings, and times how long it all takes.

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace ConsoleApplication
{
    class Program
    {
        private static Random r = new Random();

        // Create a string with randomly chosen letters, of a randomly chosen
        // length between the given min and max.
        private static string RandomString(int minLength, int maxLength)
        {
            StringBuilder b = new StringBuilder();

            int length = r.Next(minLength, maxLength);
            for (int i = 0; i < length; ++i)
            {
                b.Append(Convert.ToChar(65 + r.Next(26)));
            }

            return b.ToString();
        }

        static void Main(string[] args)
        {
            int             stringCount = 10000;                    // number of random strings to generate
            StringBuilder   pattern     = new StringBuilder();      // our regular expression under construction
            HashSet<String> strings     = new HashSet<string>();    // a set of the random strings (and their
                                                                    // prefixes) we created, for verifying the
                                                                    // regex correctness

            // generate random strings, track their prefixes in the set,
            // and add their prefixes to our regular expression
            for (int i = 0; i < stringCount; ++i)
            {
                // make a random string, 2-5 chars long
                string nextString = RandomString(2, 5);

                // for each prefix of the random string...
                for (int prefixLength = 1; prefixLength <= nextString.Length; ++prefixLength)
                {
                    string prefix = nextString.Substring(0, prefixLength);

                    // ...add it to both the set and our regular expression pattern
                    if (!strings.Contains(prefix))
                    {
                        strings.Add(prefix);
                        pattern.Append(((pattern.Length > 0) ? "|" : "") + "^" + prefix + "$");
                    }
                }
            }

            // create a regex from the pattern (and time how long that takes)
            DateTime regexCreationStartTime = DateTime.Now;
            Regex r = new Regex(pattern.ToString());
            DateTime regexCreationEndTime = DateTime.Now;

            // make sure our regex correcly matches all the strings, and their
            // prefixes (and time how long that takes as well)
            DateTime matchStartTime = DateTime.Now;
            foreach (string s in strings)
            {
                if (!r.IsMatch(s))
                {
                    Console.WriteLine("uh oh!");
                }
            }
            DateTime matchEndTime = DateTime.Now;

            // generate some new random strings, and verify that the regex
            // indeed does not match the ones it's not supposed to.
            for (int i = 0; i < 1000; ++i)
            {
                string s = RandomString(2, 5);

                if (!strings.Contains(s) && r.IsMatch(s))
                {
                    Console.WriteLine("uh oh!");
                }
            }

            Console.WriteLine("Regex create time: {0} millisec", (regexCreationEndTime - regexCreationStartTime).TotalMilliseconds);
            Console.WriteLine("Average match time: {0} millisec", (matchEndTime - matchStartTime).TotalMilliseconds / stringCount);

            Console.ReadLine();
        }
    }
}

On an Intel Core2 box I'm getting the following numbers for 10,000 strings:

Regex create time: 46 millisec
Average match time: 0.3222 millisec

When increasing the number of strings 10-fold (to 100,000), I'm getting:

Regex create time: 288 millisec
Average match time: 1.25577 millisec

This is higher, but the growth is less than linear.

The app's memory consumption (at 10,000 strings) started at ~9MB, peaked at ~23MB that must have included both the regex and the string set, and dropped to ~16MB towards the end (garbage collection kicked in?) Draw your own conclusions from that -- the program doesn't optimize for teasing out the regex memory consumption from the other data structures.

Oren Trutner
  • 23,752
  • 8
  • 54
  • 55
  • Thanks Oren it was a great help. Results on my machine confirm yours. Surprisingly Average match time is .421221 millisec for 140000 strings that vary in length from 1 to 20 characters. now i m going to read my strings from db and create a pattern string like urs that i will save in db and will reuse every time i run my application. The pattern string of course will be updated when new strings are added into the db and it is not a frequent case. plz let me know if i am on right track. Unluckily, i don't have enough points to vote up ur answer but i appreciate this great help nevertheless. – Muhammad Adeel Zahid May 29 '10 at 13:52
  • When storing the pattern in the database, keep in mind its size -- its length is going to be a multiple of all the strings that were already there. If that's an issue, the options you have available to you include recreating the pattern each time, and compressing it. The pattern should compress quite well, as it's pretty much made of prefixes; look at the System.IO.Compression if needed. – Oren Trutner May 29 '10 at 16:52
  • Thanks Oren. i will get back to it in few days – Muhammad Adeel Zahid Jun 09 '10 at 07:27