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

  • Home
  • SEARCH
  • 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 3390868
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T03:44:28+00:00 2026-05-18T03:44:28+00:00

I have an array of words I need to do a find-and-replace by regex

  • 0

I have an array of words I need to do a find-and-replace by regex operation on, and sometimes this array can be thousands of words long. I’ve tested and found that stemming the words using common prefixes is much faster than searching for them individually. That is, ^where|why$ is slower than ^wh(ere|y)$. Obviously it’s not a noticeable difference in such a short example, but it’s considerably faster where there are thousands of alternatives and the subject string is long.

So I’m looking for a way to do this stemming automatically, for instance to convert a string[] { "what", "why", "where", "when", "which" } into wh(at|y|e(re|n)|i(ch))

Is there already a recognized algorithm out there that does this ? If not, how would you go about it ? It seems to need to be done recursively but I can’t quite get my head round how to do it. I have a method I wrote that works to a limited extent, but it’s inelegant, 60 lines longs and uses multiple nested foreach loops so it’s a future maintenance nightmare. I’m sure there’s a much better way, if anyone could point me in the right direction that’d be much appreciated…

  • 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-18T03:44:28+00:00Added an answer on May 18, 2026 at 3:44 am

    This code should work:

    public static class StemmingUtilities
    {
        private class Node
        {
            public char? Value { get; private set; }
            public Node Parent { get; private set; }
            public List<Node> Children { get; private set; }
            public Node(char? c, Node parent)
            {
                this.Value = c;
                this.Parent = parent;
                this.Children = new List<Node>();
            }
        }
    
        public static string GetRegex(IEnumerable<string> tokens)
        {
            var root = new Node(null,null);
            foreach (var token in tokens)
            {
                var current = root;
                for (int i = 0; i < token.Length; i++)
                {
                    char c = token[i];
                    var node = current.Children.FirstOrDefault(x => x.Value.Value == c);
                    if (node == null)
                    {
                        node = new Node(c,current);
                        current.Children.Add(node);
                    }
                    current = node;
                }   
            }
            return BuildRexp(root);
        }
    
        private static string BuildRexp(Node root)
        {
            string s = "";
            bool addBracket = root.Children.Count > 1;
            // uncomment the following line to avoid first brakets wrapping (occurring in case of multiple root's children)
            // addBracket = addBracket && (root.Parent != null); 
            if (addBracket)
                s += "(";
            for(int i = 0; i < root.Children.Count; i++)
            {
                var child = root.Children[i];
                s += child.Value;
                s += BuildRexp(child);
                if (i < root.Children.Count - 1)
                    s += "|";
            }
            if (addBracket)
                s += ")";
            return s;
        }
    }
    

    Usage:

    var toStem1 = new[] { "what", "why", "where", "when", "which" };
    string reg1 = StemmingUtilities.GetRegex(toStem1);
    // reg1 = "wh(at|y|e(re|n)|ich)"
    
    string[] toStem2 = new[] { "why", "abc", "what", "where", "apple", "when" };
    string reg2 = StemmingUtilities.GetRegex(toStem2);
    // reg2 = "(wh(y|at|e(re|n))|a(bc|pple))"
    

    EDIT:
    to get reg2 = "wh(y|at|e(re|n))|a(bc|pple)" i.e. without the first wrapping brackets, just uncomment the marked line in BuildRexp method.

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

Sidebar

Related Questions

I have an array of shorts (short[]) that I need to write out to
I have found this example on StackOverflow: var people = new List<Person> { new
Right now I have a 2d string array holding my data: //...find number of
I am once again looking for some help. I have found this stopwords script
i have a situation, i need to process an jagged array of 20k registers
I have an array in Perl: my @my_array = (one,two,three,two,three); How do I remove
I have an array of numbers that potentially have up to 8 decimal places
I have an array of a few million numbers. double* const data = new
I have an array of items that are time sensitive. After an amount of
I have an array of hashes, and I want the unique values out of

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.