2012-04-07 183 views
1

我必须解析字符串才能创建PathSegmentCollection。该字符串由逗号和/或(任意)空格(如换行符,制表符等)分隔的数字组成,也可以使用科学记数法编写该数字。从字符串中提取数字

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

而且要点是:P1(9.63074,9.63074),P2(-0,555708,0),P3(0,1477.78)

要提取数字我使用正则表达式:

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/C++编写的使用C/C++编写的程序集就没有问题不是托管代码或C#不安全代码。

+2

您应该添加具有感兴趣语言的标签 - 我不认为很多人将“字符串”作为主题进行跟踪,而许多人则遵循C# ,VB或C++(或者您认为与您的问题相关的任何其他语言)。 – assylias 2012-04-07 10:02:47

+0

完成(.NET和C++/CLI相关),谢谢。 – gliderkite 2012-04-07 10:10:02

+0

顺便说一句,'-5.55708e-006'不是'-0,555708',它是'-0,00000555708'。 – Vlad 2012-04-07 10:13:34

回答

2

你为什么试图自己解析path markup syntax?这是一件复杂的事情,也许是将来要改变(至少延长)的一个主题。 WPF可以为你做到这一点:http://msdn.microsoft.com/en-us/library/system.windows.media.geometry.parse.aspx,所以最好让框架工作。


编辑:
如果解析是你的瓶颈,你可以尝试分析自己。我会建议尝试以下操作并检查它是否足够快:

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) ... 
} 
+0

因为不是路径标记语法。这只是一个数字序列,Parse方法不能解析我的字符串。但是,如果我更改字符串也许它可以工作,我必须测试它。 – gliderkite 2012-04-07 10:34:31

+0

@gliderkite:好吧,是不是来自某些路径描述的字符串?如果不是,则尝试在“L”前加上字符串,以便它成为有效的路径。 – Vlad 2012-04-07 10:43:33

+0

我必须插入'M'作为第一个字符,'L'作为第二个数字,'z'作为最后一个字符。任何想法? – gliderkite 2012-04-07 10:47:42