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 4104526
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T21:07:45+00:00 2026-05-20T21:07:45+00:00

I have a small project where I have an input sentence where it is

  • 0

I have a small project where I have an input sentence where it is possible for the user to specify variations:

The {small|big} car is {red|blue}

Above is a sample sentence i want to split into 4 sentences, like this:

  • The small car is red
  • The big car is red
  • The small car is blue
  • The big car is blue

I can’t seem to wrap my mind around the problem. Maybe someone can helt me pls.

Edit
Here is my initial code

Regex regex = new Regex("{(.*?)}", RegexOptions.Singleline);
MatchCollection collection = regex.Matches(richTextBox1.Text);
string data = richTextBox1.Text;

//build amount of variations
foreach (Match match in collection)
{
    string[] alternatives = match.Value.Split(new char[] { '|', '{', '}' }, StringSplitOptions.RemoveEmptyEntries);
    foreach (string alternative in alternatives)
    {
        //here i get problems                  
    }
}
  • 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-20T21:07:46+00:00Added an answer on May 20, 2026 at 9:07 pm

    It sounds like you need a dynamic cartesian function for this. Eric Lippert’s blog post written in response to Generating all Possible Combinations.

    Firstly, we need to parse the input string:

    Regex ex = new Regex(@"(?<=\{)(?<words>\w+(\|\w+)*)(?=\})");
    var sentence = "The {small|big} car is {red|blue}";
    

    then the input string should be modified to be used in string.Format-like functions:

    int matchCount = 0;
    var pattern = ex.Replace(sentence, me => 
    {
        return (matchCount++).ToString(); 
    });
    // pattern now contains "The {0} car is {1}"
    

    then we need to find all the matches and to apply Eric’s excellent CartesianProduct extension method:

    var set = ex.Matches(sentence)
        .Cast<Match>()
        .Select(m => 
            m.Groups["words"].Value
                .Split('|')
        ).CartesianProduct();
    
    foreach (var item in set)
    {
        Console.WriteLine(pattern, item.ToArray());
    }
    

    this will produce:

    The small car is red
    The small car is blue
    The big car is red
    The big car is blue

    and, finally, the CartesianProduct method (taken from here):

    static IEnumerable<IEnumerable<T>> CartesianProduct<T>(
        this IEnumerable<IEnumerable<T>> sequences) 
    { 
      IEnumerable<IEnumerable<T>> emptyProduct = new[] { Enumerable.Empty<T>() }; 
      return sequences.Aggregate( 
        emptyProduct, 
        (accumulator, sequence) =>  
          from accseq in accumulator  
          from item in sequence  
          select accseq.Concat(new[] {item}));                
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a small project that will have 1 user on 1 computer. The
I have a small project to record some form input on a page that's
I have a small project that basically a Python wrapper to a websites API.
I have a small project I want to try porting to Python 3 -
I have a small project I am doing in Python using web.py. It's a
I have a small project that require a storage (I choose SQLite) and I
I have a small project that I will be working on shortly that collects
I have a small software project with a couple clients. What I'm looking for
I have a small iPhone Project with a UITextView on my View, designed in
I have a C++ small project using GNU Make. I'd like to be able

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.