0

我正在使用带有正则表达式的 vbscript 来解析文件,这样我就可以转换一堆坐标。这是我拥有的当前代码:

Class cFormat
Private m_oSB
Private Sub Class_Initialize()
  Set m_oSB = CreateObject("System.Text.StringBuilder")
End Sub ' Class_Initialize

Public Function formatOne(sFmt, vElm)
 m_oSB.AppendFormat sFmt, vElm
  formatOne = m_oSB.ToString()
  m_oSB.Length = 0
End Function ' formatOne

Public Function formatArray(sFmt, aElms)
  m_oSB.AppendFormat_4 sFmt, (aElms)
  formatArray = m_oSB.ToString()
  m_oSB.Length = 0
End Function ' formatArray

End Class ' cFormat

Dim goFS : Set goFS = CreateObject("Scripting.FileSystemObject")

Dim oFmt : Set oFmt = New cFormat

Dim oRE  : Set oRE  = New RegExp
oRE.Global    = True
oRE.Multiline = True
'                 0     1        2        3
oRE.Pattern = "^(\d+)d(\d+)'(\d+\.\d+)""([WN])$"

Dim sALL : sALL = goFS.OpenTextFile("C:\...\lat1.txt").ReadAll()
WScript.Echo sALL

WScript.Echo oFmt.formatArray( _
  "|{0,-5}|{1,-11}|{2,-11}|{3,-15}|" _
, Array("LoLa", "Degrees", "Minutes", "Seconds") _
)

 Dim oMTS : Set oMTS = oRE.Execute(sALL)

Dim oMT
For Each oMT In oMTS
  Dim sLoLa    : sLoLa    =      oMT.SubMatches(3)
  Dim nDegrees : nDegrees = CDbl(oMT.SubMatches(0))
  Dim nMinutes : nMinutes = CDbl(oMT.SubMatches(1))
  Dim nSeconds : nSeconds = CDbl(oMT.SubMatches(2))

WScript.Echo oFmt.formatArray( _
      "|{0,-5}|{1,11:N2}|{2,11:N2}|{3,15:N6}|" _
    , Array(sLoLa, nDegrees, nMinutes, nSeconds) _
)
Next

我基于这个答案

我遇到的问题是,循环中的所有变量都是空的,所以在最后一个回显中我什么也没有得到。我似乎无法确定问题,是正则表达式吗?周期?我真的不知道。

lat1.txt 文件是一个简单的 txt 文件,包含以下几行:

41d3'40.313"N  
38d42'29.295"N  
41d28'13.616"N  

第一个回显语句正在工作......问题出在最后一个。

有人可以帮助我吗?

在此先感谢,克劳迪奥

4

1 回答 1

1

这是您的模式,将其替换为

oRE.Pattern = "^(\d+)d(\d+)\'(\d+\.\d+)""(N|W) *$" 

给出以下,秒的输出在我的系统上不正确,但这可能是区域设置

41d3'40.313"N   
38d42'29.295"N   
41d28'13.616"N   

|LoLa |Degrees    |Minutes    |Seconds        |
|N    |      41,00|       3,00|  40.313,000000|
|N    |      38,00|      42,00|  29.295,000000|
|N    |      41,00|      28,00|  13.616,000000|
于 2012-06-06T11:00:21.863 回答