Extraction and reading of decimal numbers of a string in C #

advertisements

I am relatively new to C# programming and I apologize if this is a simple matter, but I need help with something.

I need a function which will 'extract' regular AND decimal numbers from a string and place them in an array. I'm familiar with

    string[] extractData = Regex.Split(someInput, @"\D+")

but that only takes out integers. If I have a string "19 something 58" it will take 19 and 58 and store them into two different array fields. However if I had "19.58 something" it will again take them as two separate numbers, while I want to register it as one decimal number.

Is there a way to make it 'read' such numbers as one decimal number, using Regex or some other method?

Thanks in advance.


Try following :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace ConsoleApplication9
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            string[] inputs = {
                                 "9 something 58" ,
                                 "19.58 something"
                             };

            foreach (string input in inputs)
            {
                MatchCollection matches = Regex.Matches(input, @"(?'number'\d*\.\d*)|(?'number'\d+[^\.])");
                foreach (Match match in matches)
                {
                    Console.WriteLine("Number : {0}", match.Groups["number"].Value);
                }
            }
            Console.ReadLine();

        }
    }

}