Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regex - Search within section

Status
Not open for further replies.

stevio

Vendor
Jul 24, 2002
78
AU
Hi all,

I'm trying to build a regex which will allow me to search a text file between sections

So the data looks like this, the data begins after
Data1 Data2 Data3 and ends when the next line is:

Data4 Data5 Data6

---- Section 2 ----
Data1 Data2 Data3
0 0 30
0 1 0
0 2 76
0 3 0
...
1 0 88
1 1 54
1 2 45
1 3 3
...
Data4 Data5 Data6

What I am trying to do is compare rows of data between the top half and bottom half, which changes when it goes from 0 to 1 in Data1 column. Data2 numbers are a mirror of each other. Data2 doesn't necessarily start from 0. If Data3 is greater than 0 in the top AND bottom halves, I want to write the results to a file.

I can't just use a normal regex to look for the numbers because other parts of the file might have the same format/spaces etc. The only things unique are the section headings and the following 'Data' column headings.

So in the example above for first line of each half, it happens to be what we are looking for:
0 0 30
1 0 88

So far, here's what I come up with, haven't written anything to a file yet as I haven't got it to work.

Code:
option explicit
'On Error Resume Next

Dim objFile, objFSO, objFile2
Dim LineDetails, Regex
Dim StartFlag, EndFlag

Const ForReading = 1, ForWriting = 2, ForAppending = 8

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("data.txt")

Do While Not objFile.AtEndOfStream
    LineDetails = objFile.ReadLine
    If LineDetails = "Data1   Data2   Data3" Then
        StartFlag = 1
	 Wscript.echo "DATA1" & vbTab & vbTab & "DATA2" & vbTab & vbTab & "DATA3"
    End If

    If StartFlag = 1 Then
        If LineDetails = "Data4   Data5   Data6" Then
            EndFlag = 1
            StartFlag = 0
        Else
            If Len(LineDetails) <> 0 Then
                  Set RegEx = New RegExp
                  Regex.Pattern = "^\s{2}(\d+?)\s+?\(d+?)\s+(\d+)"
                  RegEx.Global = True
                  RegEx.IgnoreCase = True                
                  Set colmatches = objFile.Readall       
                  For Each objMatch In colmatches                   
                        Wscript.echo objmatch.submatches(0) & vbtab & vbtab & objmatch.submatches(1) & vbtab & vbtab & _   objmatch.submatches(2)                      
                  Next
            End If
        End If
    End If

    If EndFlag = 1 Then
        StartFlag = 0
        EndFlag = 0
    End If
Loop

When I run this, I get a runtime error:

VBScript runtime error: Object required: '[string: " 0 0 "]

Any ideas?
Any help is appreciated

Stevio
 
Set colmatches = objFile.Readall
The above line seems very strange to me ...

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top