在大字符串中查找浮点

Joe*_*man 2 c# regex string floating-point double

我有一个大字符串,在字符串中有一系列浮点数.一个典型的字符串会有Item X $4.50 Description of item \r\n\r\n Item Z $4.75...文本真的没有押韵或理由.我已经是最低的,我需要找到字符串中的所有值.因此,如果是,10.00它会发现每个10.05或更少的价值.我会假设某种正则表达式会涉及到找到值,然后我可以将它们放在一个数组中然后对它们进行排序.

因此,找到哪些值符合我的标准就是这样的.

int [] array;
int arraysize;
int lowvalue;
int total;

for(int i = 0; i<arraysize; ++i)
{
    if(array[i] == lowvalue*1.05) ++total;
}
Run Code Online (Sandbox Code Playgroud)

我的问题是在数组中获取这些值.我已经读过这个但是d +并不适用于浮点数.

Ice*_*ind 5

您应该使用RegEx:

Regex r = new RegEx("[0-9]+\.[0-9]+");
Match m = r.Match(myString);
Run Code Online (Sandbox Code Playgroud)

这样的事情.然后你可以使用:

float f = float.Parse(m.value);
Run Code Online (Sandbox Code Playgroud)

如果你需要一个数组:

MatchCollection mc = r.Matches(myString);
string[] myArray = new string[mc.Count];
mc.CopyTo(myArray, 0);
Run Code Online (Sandbox Code Playgroud)

编辑

我刚刚为你创建了一个小样本应用程序Joe.我编译了它,并且使用你问题中的输入行在我的机器上工作正常.如果您遇到问题,请发布您的InputString,以便我可以尝试使用它.这是我写的代码:

static void Main(string[] args)
{
    const string InputString = "Item X $4.50 Description of item \r\n\r\n Item Z $4.75";

    var r = new Regex(@"[0-9]+\.[0-9]+");
    var mc = r.Matches(InputString);
    var matches = new Match[mc.Count];
    mc.CopyTo(matches, 0);

    var myFloats = new float[matches.Length];
    var ndx = 0;
    foreach (Match m in matches)
    {
        myFloats[ndx] = float.Parse(m.Value);
        ndx++;
    }

    foreach (float f in myFloats)
        Console.WriteLine(f.ToString());

    // myFloats should now have all your floating point values
}
Run Code Online (Sandbox Code Playgroud)