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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:08:15+00:00 2026-05-30T18:08:15+00:00

I have to parse a string. It contains one or two numbers. It can

  • 0

I have to parse a string. It contains one or two numbers.

It can has 3 froms.

  1. {number1}-{number2}
  2. {number1}-
  3. -{number2}

The numbers can be negative.

So for example:

  • 100-200 means: number1 = 100, number2 = 200
  • -100 means: number1 = null, number2 = 100
  • 200- means: number1 = 200, number2 = null

now the harder part:

  • -100-200 means: number1 = -100, number2 = 200
  • -100- means: number1 = -100, number2 = null
  • –100 means: number1 = null, number2 = -100
  • 100–200 means: number1 = 100, number2 = -200
  • -100–200 means: number1 = -100, number2 = -200

  • -100-200- means: throw new FormatException(). its not a valid string.

Anything that cannot be parsed as 2 ints throws a formatexception (int.Parse does this, so it can be depended on).

I need a parser to parse it to two int? -s (nullable ints).

Every string is either valid (it makes sense), or invalid (it does not makes sense). There is no string that can mean 2 things (at least I didn’t find one).

If I het the 2 value, I want it to return in a tuple.

I get tangled up in the ifs.

  • 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-30T18:08:17+00:00Added an answer on May 30, 2026 at 6:08 pm

    (Editing to add the updated regex from the comments)

    It seems like you could use the Regex class to figure this out, since your syntax is fairly regular and structures:

    var regex = new Regex(@"^(?<first>-?[\d.,]+)?-(?<second>-?[\d.,]+)?$");
    var match = regex.Match(input);
    if (match.Success)
    {
      int? a = match.Groups["first"].Success
        ? Int32.Parse(match.Groups["first"].Value) 
        : (int?)null;
    
      int? b = match.Groups["second"].Success
        ? Int32.Parse(match.Groups["second"].Value) 
        : (int?)null;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string: [\n['-','some text what\rcontains\nnewlines'],\n\n trying to parse: Regex.Split(@[\n['-','some text what contains
I have a function which parses one string into two strings. In C# I
I have a String that I have to parse for different keywords. For example,
I have some sort of recursive function, but I need to parse a string,
I have a string like this that I need to parse into a 2D
I have a large string I need to parse, and I need to find
I have a date string and I want to parse it to normal date
Aloha I have a method with (pseudo) signature: public static T Parse<T>(string datadictionary) where
I have a combo box that contains 5 items. I have one label on
I want to break down a JSON string into smaller objects. I have two

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.