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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:04:38+00:00 2026-06-17T05:04:38+00:00

I’m working on a WinForms app that accepts numeric strings as an input. If

  • 0

I’m working on a WinForms app that accepts numeric strings as an input. If my user is entering a contiguous range of numbers, he/she may represent that range using a shorthand notation like this:

12[3-5]5550[000-100]

Using that example, I need to expand that string into the entire range of numbers that it encompasses like this:

1235550000
1235550001
1235550002
..........
1235550100

1245550000
1245550001
1245550002
..........
1245550100

1255550000
1255550001
1255550002
..........
1255550100

With the exception of 0-9, the strings will only contain [, – and ] which are the characters used to represent a range. As a part of the process of expanding the example string into individual numbers, I would like to validate the string, as well. For example, there must be an equal number of [ and ] characters. Inside of the brackets, the first number in the range must be less than the second number in the range. These types of things.

Is there an existing pattern that performs this kind of string manipulation?

  • 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-06-17T05:04:39+00:00Added an answer on June 17, 2026 at 5:04 am

    There is nothing built in library in .net, that you can use. Your task is quite complex, considering that you want validation as well. I have created a small solution for generation of strings based on your initial input. Note this isn’t a production code and I have omitted some checks. This code only shown’s one possible way of how it could be done and is open to critique. Token represent each logical piece of your string, which in your case either a literal, or a range specifier.

    class Token
    {
        public string Value { get; set; }
    
        public IEnumerable<string> GetAllValues()
        {
            if(IsRange(Value))
            {
                var rangeValues = Value.Split(new[] {'[', '-', ']'}, StringSplitOptions.RemoveEmptyEntries);
                //numeric format defines minimal string length. [01-10] will have numericFormat = "{0:00}"
                string numericFormat = CreateNumericFormat(rangeValues.ElementAt(0).Length);
    
                int[] ranges = rangeValues.Select(val => int.Parse(val)).ToArray();
    
                foreach (var val in GetRange(ranges[0], ranges[1]))
                    yield return string.Format(numericFormat, val);
            }
            else
            {
                yield return Value;
            }
        }
        //validation is ommited
        private bool IsRange(string val)
        {
            return Value.Contains("-");
        }
        private string CreateNumericFormat(int minimalLength)
        {
            return "{0:"+new string('0', minimalLength)+"}";
        }
        private IEnumerable<int> GetRange(int minValue, int maxValue)
        {
            return Enumerable.Range(minValue, maxValue - minValue + 1);
        }
    }
    

    Now usage is quite straightforward:

    var initial = "12[05-10]3[008-010]";
    
    var regularExp = @"\[\s*\d+\s*-\s*\d+\s*\]|\d+";
    
    List<Token> tokens = new List<Token>();
    
    
    foreach (Match match in Regex.Matches(initial , regularExp ))
    {
        tokens.Add(new Token {Value = match.Value});
    }
    
    
    IEnumerable<string> result = new []{""};//empty source for start
    
    foreach(var token in tokens)
    {
        result = from v1 in result
                 from v2 in token.GetAllValues()
                 select v1 + v2;
    }
    //Dump is LinqPad specific call
    result.Dump();
    

    Prints:

    12053008 
    12053009 
    12053010 
    12063008 
    12063009 
    12063010 
    12073008 
    12073009 
    12073010 
    12083008 
    12083009 
    12083010 
    12093008 
    12093009 
    12093010 
    12103008 
    12103009 
    12103010 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I need to clean up various Word 'smart' characters in user input, including but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

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.