0

i'm using a vbscript with regex to parse a file so i can convert a bunch of coordinates. This is the current code i have:

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

I'm basing myself on this answer.

The problem i'm having is that, all the variables in the cycle are empty, so in the last echo i don't get nothing. I can't seem to identify the problem, is the regular expression? the cycle? I really don't know.

The lat1.txt file is a simple txt file with the following lines:

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

The first to echo statements are working... the problem is in the last one.

Can somebody help me?

Thanks in advance, Cláudio

Community
  • 1
  • 1
Cláudio Ribeiro
  • 1,509
  • 3
  • 37
  • 65

1 Answers1

1

It is your pattern, replace it with

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

gives following, the output of the seconds is not correct on my system, but that could be regional settings

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|
peter
  • 41,770
  • 5
  • 64
  • 108
  • Thanks a lot! Can you tell me what was wrong with the patterns, so i can understand the mistake. – Cláudio Ribeiro Jun 06 '12 at 11:03
  • special characters like the ' need to be escaped by \ and you need to trap ending spaces wen you include the $ to match the end of the line so i put one space followed by * for zer0 or more occurances. The NW was not i problem i think but in this case the (N|W) is shorter – peter Jun 06 '12 at 16:07