I have to parse a string. It contains one or two numbers.
It can has 3 froms.
- {number1}-{number2}
- {number1}-
- -{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.
(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: