您好,我的一穿字符中包含一个坐标点信息,如下END-POINT 46105.000 3048.000 0.000 10其中END-POINT是点的名字,后面的三位是点坐标,X,Y,Z,中间隔4个空格。最后的10是一个物件尺寸信息,请教您,我现在如何通过一个正则表达式把坐标点抽出来,并且把坐标点的名字、和尺寸也顺便抽出来以便对应?感谢指导。
调试欢乐多
而不能把值分别赋给某几个变量。
赋值的工作,你完全可以使用split函数来拆分字符串完成。
也就是说,那个用VB完成的事。
如果你想得到进一步的帮助,可以把整个文件的内容上传,或者粘贴出来。
Option Explicit
Private Type PointInfo
Name As String '点名
PX As String 'X坐标
PY As String 'Y坐标
PZ As String 'Z坐标
Size As String '尺寸
End Type
Private PointInfo() As PointInfoPrivate Sub Command1_Click()
Dim testStr$
Dim i&
testStr = "END-POINT1 46105.35 3048.0 35.006 10" & vbCrLf & _
"END-POINT2 354.53 3535.000 2.500 25" & vbCrLf & _
"END-POINT3 389.000 853.053 83.354 66" & vbCrLf & _
"END-POINT4 354.000 35.03 75.035 35"
Call GetPointInfo(testStr) '处理
For i = LBound(PointInfo) To UBound(PointInfo) '检查结果
Debug.Print PointInfo(i).Name & "|" & PointInfo(i).PX & "|" & PointInfo(i).PY & "|" & PointInfo(i).PZ & "|" & PointInfo(i).Size
Next i
End Sub
Public Sub GetPointInfo(ByVal Source$)
On Error Resume Next
Dim RegExp As Object, Matches As Object
Dim i&
Set RegExp = CreateObject("VBscript.RegExp")
RegExp.Global = True
RegExp.Pattern = "(.*?) (\d+\.\d+) (\d+\.\d+) (\d+\.\d+) (\d+)" '这里仅以1个空格为例,如为多个不固定空格,处理后再用
Set Matches = RegExp.Execute(Source)
ReDim PointInfo(Matches.Count - 1)
For i = 0 To Matches.Count - 1
PointInfo(i).Name = Matches(i).SubMatches(0)
PointInfo(i).PX = Matches(i).SubMatches(1)
PointInfo(i).PY = Matches(i).SubMatches(2)
PointInfo(i).PZ = Matches(i).SubMatches(3)
PointInfo(i).Size = Matches(i).SubMatches(2)
Next i
Set RegExp = Nothing
Set Matches = Nothing
End Sub
测试结果:
END-POINT1|46105.35|3048.0|35.006|3048.0
END-POINT2|354.53|3535.000|2.500|3535.000
END-POINT3|389.000|853.053|83.354|853.053
END-POINT4|354.000|35.03|75.035|35.03
不知能否满足LZ要求?
PointInfo(i).Size = Matches(i).SubMatches(2)
改成:PointInfo(i).Size = Matches(i).SubMatches(4)。