2

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.

vinczemarton
  • 7,756
  • 6
  • 54
  • 86

3 Answers3

8

(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;
}
TDaver
  • 7,164
  • 5
  • 47
  • 94
Michael Edenfield
  • 28,070
  • 4
  • 86
  • 117
  • This is great! I'll also need to parse decimals, which are culture invariant (so I can expect 5.02 but also 10,5). Can you help escaping these 2 characters too? – vinczemarton Mar 02 '12 at 18:10
  • 3
    @SoonDead: Regex(@"^(?-?[\d\.\,]+)?-(?-?[\d\.\,]+)?$") – mrbm Mar 02 '12 at 18:14
  • I have learned a great deal about using regex in C# from this. Like named groups. Thanks a lot. – vinczemarton Mar 02 '12 at 18:37
  • 1
    @mrbm: small nitpick: I don't think you need to escape `.` and `,` if inside `[]`. Not a mistake though... – TDaver Mar 22 '12 at 14:59
6

Have you considered using a regular expression?

^(-?\d+)?-(-?\d+)?$
dtb
  • 213,145
  • 36
  • 401
  • 431
  • Can you explain this a little more? If you can solve this with regular expressions and return the two int?-s, then it is great. – vinczemarton Mar 02 '12 at 18:02
  • @SoonDead: Have a look at the answer by Michael Edenfield. It's the same regular expression, but he also shows how to use it in C#. – dtb Mar 02 '12 at 18:03
  • @SoonDead you aren't going to get two int?s out of a regex -- it parses strings -- but once you get the string split into pieces it should be pretty easy from there. – Michael Edenfield Mar 02 '12 at 18:05
-2

you could try .Split() method:

string testString = "100-200" ;

string[] results = testString.Split('-');

 int nr1;
 int nr2;
try
{
  nr1  = Convert.toInt32(results[0]);
  nr2  = Convert.toInt32(results[1]);
}
catch{}
Alex Peta
  • 1,407
  • 1
  • 15
  • 26