Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 854667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:57:36+00:00 2026-05-15T07:57:36+00:00

I have about 100,000 strings in database and I want to if there is

  • 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T07:57:37+00:00Added an answer on May 15, 2026 at 7:57 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a few strings(about 100) and I want to store them in a
There are about 100,000 different possible ways to encrypt a string. Using standards like
I have a .txt file with about 100,000 points in the 2-D plane. When
I have about a 100 csv files each 100,000 x 40 rows columns. I'd
We have a text file with about 100,000 rows, about 50 columns per row,
I have a list of approx. 500,000 strings, each approx. 100 characters long. Given
I have a text file with about 100,000 lines (5 MB), which is updated
I have about 100 Word documents which include transliteration of foreign names. The author
I have about 100 tables where all of them have duplicate foreign key constraints
I have about 100 stylesheets that apply the same style to one document, exactly

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.