从字符串中提取号码 [英] Extract numbers from String

查看:189
本文介绍了从字符串中提取号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要解析字符串,创建一个的 PathSegmentCollection 。该字符串由用逗号分隔的数字和/或(任何)空格(如换行,标签,等...),还有数字可以用科学记数法写。

I have to parse a String to create a PathSegmentCollection. The string is composed of numbers separated by comas and/or (any) whitespaces (like newline, tab, etc...), also the numbers can be written using scientific notation.

这是一个例子:9.63074,9.63074 -5.55708e-006 0,0 1477.78

和的要点是:P1(9.63074,9.63074),P2(-0,555708,0),P3(0,1477.78)

And the points are: P1(9.63074, 9.63074), P2(-0,555708, 0), P3(0, 1477.78)

要提取我用一个普通的前pression号:

To extract numbers I use a regular expression:

Dim RgxDouble As New Regex("[+-]?\b[0-9]+(\.[0-9]+)?(e[+-]?[0-9]+)?\b")
Dim Matches As MatchCollection = RgxDouble.Matches(.Value)
Dim PSegmentColl As New PathSegmentCollection
Dim PFigure As New PathFigure

With Matches

  If .Count < 2 OrElse .Count Mod 2 <> 0 Then Exit Sub

  PFigure.StartPoint = New Point(.Item(0).Value, .Item(1).Value)

  For i As UInteger = 2 To .Count - 1 Step 2
    Dim x As Double = .Item(i).Value, y As Double = .Item(i + 1).Value
    PSegmentColl.Add(New LineSegment With {.Point = New Point(x, y)})
  Next

End With

它的工作原理,但我必须解析约十万(或以上)的字符串,并以这种方式太慢。我想找到一个更有效的,而解决办法:大部分的数字写在科学记数法,如果你认为这是一个更好的办法,我没有问题,用写在C ++ / CLI使用C / C ++程序集的时代++没有管理$​​ C $ C或C#不安全code。

It works, but I have to parse about a hundred thousand (or more) strings, and in this way is too slow. I want to find a more efficient solution whereas: most of the times the numbers are not written in scientific notation and, if you think that's a better way, I have no problem to use an assembly written in C++/CLI that use C/C++ not managed code, or C# unsafe code.

推荐答案

为什么你想解析的路径标记自己的语法?这是一个复杂的事情,也许是受到改变(至少扩展)的未来。 WPF可以为你做到这一点:的http:// msdn.microsoft.com/en-us/library/system.windows.media.geometry.parse.aspx ,所以最好是让框架的工作。

Why are you trying to parse the path markup syntax yourself? It's a complicated thing, and perhaps a subject to be changed (at least extended) in the future. WPF can do this for you: http://msdn.microsoft.com/en-us/library/system.windows.media.geometry.parse.aspx, so it's better to let the framework work.

编辑:
如果分析是瓶颈,您可以尝试解析自己。我会建议您尝试以下和检查,如果它的速度不够快:


If the parsing is your bottleneck, you can try to parse yourself. I would recommend trying the following and checking if it's fast enough:

char[] separators = new char[] { ' ', ',' }; // should be created only once
var parts = pattern.Split(separators, StringSplitOptions.RemoveEmptyEntries);
double firstInPair = 0.0;
for (int i = 0; i < parts.Length; i++ )
{
    double number = double.Parse(parts[i]);
    if (i % 2 == 0)
    {
        firstInPair = number;
        continue;
    }
    double secondInPair = number;
    // do whatever you want with the pair (firstInPair, secondInPair) ...
}

这篇关于从字符串中提取号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆