3

i have this C# code that i found and then improved upon for my needs, but now i would like to make it work for all numeric data types.

    public static int[] intRemover (string input)
    {
        string[] inputArray = Regex.Split (input, @"\D+");
        int n = 0;
        foreach (string inputN in inputArray) {
            if (!string.IsNullOrEmpty (inputN)) {
                n++;
            }
        }
        int[] intarray = new int[n];
        n = 0;
        foreach (string inputN in inputArray) {
            if (!string.IsNullOrEmpty (inputN)) {
                intarray [n] = int.Parse (inputN);
                n++;
            }
        }
        return intarray;
    }

This works well for trying to extract whole number integers out of strings but the issue that i have is that the regex expression i am using is not setup to account for numbers that are negative or numbers that contain a decimal point in them. My goal in the end like i said is to make a method out of this that works upon all numeric data types. Can anyone help me out please?

9
  • 1
    You will definitely want to move away from using the int datatype then. Perhaps use decimal. Unfortunately my Regex isn't up to scratch so I cannot help you with an answer Commented Nov 30, 2012 at 8:50
  • When you say "all numeric data types" do you want it to handle exponents, hexa-decimal etc? or do you just need +/- and the decimal point handling? Commented Nov 30, 2012 at 8:50
  • NumberExtracter would be a better name. How would you return those numbers? As you can't return a list of int with double in the middle. Also how big can these numbers get? Maybe you'd need to create a custom class that can hold any number. Commented Nov 30, 2012 at 8:51
  • what would your input look like Commented Nov 30, 2012 at 8:53
  • @musefan i am making similar methods like this for each data type, i just used the int version for the post. Commented Nov 30, 2012 at 9:04

2 Answers 2

6

You can match it instead of splitting it

public static decimal[] intRemover (string input)
{
    return Regex.Matches(input,@"[+-]?\d+(\.\d+)?")//this returns all the matches in input
                .Cast<Match>()//this casts from MatchCollection to IEnumerable<Match>
                .Select(x=>decimal.Parse(x.Value))//this parses each of the matched string to decimal
                .ToArray();//this converts IEnumerable<decimal> to an Array of decimal
}

[+-]? matches + or - 0 or 1 time

\d+ matches 1 to many digits

(\.\d+)? matches a (decimal followed by 1 to many digits) 0 to 1 time


Simplified form of the above code

    public static decimal[] intRemover (string input)
    {
        int n=0;
        MatchCollection matches=Regex.Matches(input,@"[+-]?\d+(\.\d+)?");
        decimal[] decimalarray = new decimal[matches.Count];

        foreach (Match m in matches) 
        {
                decimalarray[n] = decimal.Parse (m.Value);
                n++;
        }
        return decimalarray;
    }
Sign up to request clarification or add additional context in comments.

16 Comments

i'm still pretty new to c# and programming in general, could you give me a quick rundown of what is going on with that code? also i think i may be missing the proper reference to use this code, i dropped it in and it couldnt find definitions for ".cast" and a few other things. what do i need to reference to make it work?
@AlexZywicki you need to include System.Linq
its throwing an exception because the array index is out of bounds. in theory i could just make the array bigger, but the way i had it setup before worked for in a way that the array size was decided based on the result of the regex.split that way it could account for almost any size of input and aways come back within the bounds of the array.
the code runs now but it is still removing the decimal point, but it is accepting negatives! so that solves half of the issue
@AlexZywicki numbers like 444. 554. doesnt make sense cuz the . is redundant..
|
1

try modifying you regular expression like this:

 @"[+-]?\d+(?:\.\d*)?"

1 Comment

Indeed. Thanks @Rawling, I had it escaped, but Stackoverflow ate my backslash. I had to escape that too.

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.