0

I'm trying out services in C#, so I cannot debug values using Console.WriteLine(...). That's why I don't know what is in my variable number at the end:

    public int addAndSoustractNumberFromAString(string ustr)
    {
        int result = 0;

        if(!string.IsNullOrEmpty(str))
        {
            string str = ustr.Trim();

            //Regexes
            System.Text.RegularExpressions.Regex valid_regex = new System.Text.RegularExpressions.Regex(@"[\+]\d+|[\-]\d+");
            System.Text.RegularExpressions.Regex invalid_regex = new System.Text.RegularExpressions.Regex(@"[a-zA-Z]");

            //Handling errors
            if (invalid_regex.IsMatch(str) || !valid_regex.IsMatch(str) || !Char.IsDigit(str[str.Length - 1])) return 0;
            if (Char.IsDigit(str[0])) str = "+" + str;

            //Load all numbers in a string array
            List<string> numbers = new List<string>(valid_regex.Split(str).ToArray());

            //Cast string numbers to int + result calculation
            foreach (string number in numbers) { if (!int.TryParse(number, out int n)) return 99; result += n; }
        }

        return result;
    }

What does my code? I enter additions & subtractions, it does the operations & return the result as an integer.

example: If I enter "52-2+3" it should return 53

Problem: My code always returns 99 which means the parsing failed. Just in case, I tried int.TryParse("-2", out int n) instead and it works fine.

The line that is not working:

List<string> numbers = new List<string>(valid_regex.Split(str).ToArray());

My regexes are fine, I tested them with the website regexstorm net.

My question is, why my string isn't split correctly using a supposed valid regex? Is Regex.Split() not doing what I believe it does? (I thought it would split my string into +number or -number strings

Side note: it's ok if it does not start with a sign. If my string starts with a number it will add "+" at the end for regex purposes.

9
  • Why do you declare an int inside the call to TryParse ? Don't you want to get this value ? Have you tried declaring int n; then int.TryParse(number, out n) ? Commented Jul 7, 2017 at 15:12
  • Yeah, it works for my other methodes. Just in case I tested and it still shows 99. It's ok to declare it inside the function TryParse. Commented Jul 7, 2017 at 15:16
  • Couldn't you get this code into a Console Application and debug it there? Commented Jul 7, 2017 at 15:18
  • People are serious downvoting without saying anything... I tested a lot of things before posting it here... :/ No I can't, it use ISS, Console.WriteLine(...) doesn't work. Commented Jul 7, 2017 at 15:18
  • 1
    This is probably simpler: Wrapping a C# service in a console app to debug it Commented Jul 7, 2017 at 15:21

2 Answers 2

1

Your Regex is not a Split()ing Regex.

Split() expects the regular expression to define the delimiter (i.e. split on a comma or semicolon). Your regex describes the values.

It seems like you want the result of Matches() and not Split().

This seems to work for me with your example:

List<string> numbers = valid_regex.Matches(str).Cast<Match>().Select(m => m.Value).ToList();

Also, LinqPad is a wonderful tool to prototype code snippets like this one.

Sign up to request clarification or add additional context in comments.

1 Comment

It's working ! So as expected I was misunderstanding the regex.split() function. Thanks !
0

Replacing your regexp with ([\+\-]\d+) made the trick with the example "52-2+3" here.

Also added .Where(x => !String.IsNullOrEmpty(x)).ToList(); after the split so you get rid of empty strings.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.