Extracting Portion of a file from text file

I am no great programmer but I have a copy of visual basic 6 on my machine.  I have a text file one.reg (exported registry entry) but the file has a lot of hexadecimals, I need to extract only a portion of the file eg "0012902" which represents a student login name. There are many students login name in the file but they all fall between the quotes " ".  I need to extract all the idnumbers from this file to another new file on seperate lines. (eg line 1 0012902, line 2 120232).  Thanks in advance Raymond.
rwhittleAsked:
Who is Participating?
 
Patrick MatthewsConnect With a Mentor Commented:
Hi rwhittle,

I made a dummy text file, with contents:


be59g46a60d2883"1"a39a9bf26ab1fh
hbi40h89212bdfac"2"h115ahbf202g851i4d
5i8f37fg"3"af9522f067b1
9ed4ag4ccfh"4"1ff6fhh6h43h930e2
9724cea656gi161dd"5"g1g1i1a1d9896h6
8c38e97i056hdghc2bd"6"40d9iaif576dcachd
7b"7"i5ehh1f2db
h6"8"5ibhg4f6c29e8c93281e
ed"9"ga56f4
hg5eie72h"10"h7280g7e
7hc964hadd0hcf37"11"0d8
g5a056g9"12"638
64h4fgi8c3gbdbi9109e"13"hhf0c09183i4b540ce
b34aihf"14"9
1e"15"i55h85
e2g70"16"d3525d6d9bb6hiccda41
49g0h4di161i0"17"e2037hffc1
d7258g1b3h9c82gc0e9c"18"ag181h763
dgb2d8bif75f3"19"ebag6fe0ega0
81b5"20"hi0
0c0e21c830hc5i5"21"26adbcgca1316bd3igc4
486a1"22"8i300ighfbe9bhg
f"23"832ib1
hf9"24"9hfdd3hh4g3dd2bi8e5h
3a"25"1bb6faf



I then wrote this code:

' Function by Patrick Matthews

Option Explicit

Function RegExpFind(LookIn As String, PatternStr As String, Optional Pos, _
    Optional MatchCase As Boolean = True)

    ' This function uses Regular Expressions to parse a string (LookIn), and return matches to a
    ' pattern (PatternStr).  Use Pos to indicate which match you want:
    ' Pos omitted               : function returns a zero-based array of all matches
    ' Pos = 0                   : the last match
    ' Pos = 1                   : the first match
    ' Pos = 2                   : the second match
    ' Pos = <positive integer>  : the Nth match
    ' If Pos is greater than the number of matches, is negative, or is non-numeric, the function
    ' returns an empty string.  If no match is found, the function returns an empty string
   
    ' If MatchCase is omitted or True (default for RegExp) then the Pattern must match case (and
    ' thus you may have to use [a-zA-Z] instead of just [a-z] or [A-Z]).
   
    ' If you use this function in Excel, you can use range references for any of the arguments.
    ' If you use this in Excel and return the full array, make sure to set up the formula as an
    ' array formula.  If you need the array formula to go down a column, use TRANSPOSE()
   
    Dim RegX As Object
    Dim TheMatches As Object
    Dim Answer() As String
    Dim Counter As Long
   
    ' Evaluate Pos.  If it is there, it must be numeric and converted to Long
    If Not IsMissing(Pos) Then
        If Not IsNumeric(Pos) Then
            RegExpFind = ""
            Exit Function
        Else
            Pos = CLng(Pos)
        End If
    End If
   
    ' Create instance of RegExp object
    Set RegX = CreateObject("VBScript.RegExp")
    With RegX
        .Pattern = PatternStr
        .Global = True
        .IgnoreCase = Not MatchCase
    End With
       
    ' Test to see if there are any matches
    If RegX.test(LookIn) Then
       
        ' Run RegExp to get the matches, which are returned as a zero-based collection
        Set TheMatches = RegX.Execute(LookIn)
       
        ' If Pos is missing, user wants array of all matches.  Build it and assign it as the
        ' function's return value
        If IsMissing(Pos) Then
            ReDim Answer(0 To TheMatches.Count - 1) As String
            For Counter = 0 To UBound(Answer)
                Answer(Counter) = TheMatches(Counter)
            Next
            RegExpFind = Answer
       
        ' User wanted the Nth match (or last match, if Pos = 0).  Get the Nth value, if possible
        Else
            Select Case Pos
                Case 0                          ' Last match
                    RegExpFind = TheMatches(TheMatches.Count - 1)
                Case 1 To TheMatches.Count      ' Nth match
                    RegExpFind = TheMatches(Pos - 1)
                Case Else                       ' Invalid item number
                    RegExpFind = ""
            End Select
        End If
   
    ' If there are no matches, return empty string
    Else
        RegExpFind = ""
    End If
   
    ' Release object variables
    Set RegX = Nothing
    Set TheMatches = Nothing
   
End Function

Sub GrabData()

    Dim Entries As Variant
    Dim fso As Object
    Dim ts As Object
    Dim WholeFile As String
   
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set ts = fso.OpenTextFile("C:\Folder\Subfolder\blah.txt")
    WholeFile = ts.ReadAll
    ts.Close
    Set ts = Nothing
    Set fso = Nothing
   
    Entries = RegExpFind(WholeFile, """\d+""")
   
    [a:a].Clear
    [a1].Resize(UBound(Entries) + 1, 1).Value = Application.Transpose(Entries)
   
End Sub


GrabData successfully retrieved the data for me.

Regards,

Patrick
0
 
zorvek (Kevin Jones)ConsultantCommented:
Can you post the file here or a sample of the file?

Kevin
0
 
rwhittleAuthor Commented:
This is a portion of the file.  I would love to get out 0120049, 0120050, 0120052 from this file.

0120049$"=hex:01,00,04,80,ac,00,00,00,bc,00,00,00,00,00,00,00,14,00,00,00,02,\
  00,98,00,04,00,00,00,00,00,24,00,ff,01,1f,00,01,05,00,00,00,00,00,05,15,00,\
  00,00,db,eb,0c,50,57,66,e2,62,82,8b,a6,28,e3,04,00,00,00,00,24,00,ff,01,1f,\
  00,01,05,00,00,00,00,00,05,15,00,00,00,79,e3,fc,53,44,dd,b8,3d,23,5f,63,6b,\
  0e,05,00,00,00,00,24,00,ff,01,1f,00,01,05,00,00,00,00,00,05,15,00,00,00,79,\
  e3,fc,53,44,dd,b8,3d,23,5f,63,6b,0b,05,00,00,00,00,24,00,ff,01,1f,00,01,05,\
  00,00,00,00,00,05,15,00,00,00,db,eb,0c,50,57,66,e2,62,82,8b,a6,28,77,04,00,\
  00,01,02,00,00,00,00,00,05,20,00,00,00,20,02,00,00,01,05,00,00,00,00,00,05,\
  15,00,00,00,db,eb,0c,50,57,66,e2,62,82,8b,a6,28,01,02,00,00
"0120050$"=hex:01,00,04,80,ac,00,00,00,bc,00,00,00,00,00,00,00,14,00,00,00,02,\
  00,98,00,04,00,00,00,00,00,24,00,ff,01,1f,00,01,05,00,00,00,00,00,05,15,00,\
  00,00,db,eb,0c,50,57,66,e2,62,82,8b,a6,28,e4,04,00,00,00,00,24,00,ff,01,1f,\
  00,01,05,00,00,00,00,00,05,15,00,00,00,79,e3,fc,53,44,dd,b8,3d,23,5f,63,6b,\
  0e,05,00,00,00,00,24,00,ff,01,1f,00,01,05,00,00,00,00,00,05,15,00,00,00,79,\
  e3,fc,53,44,dd,b8,3d,23,5f,63,6b,0b,05,00,00,00,00,24,00,ff,01,1f,00,01,05,\
  00,00,00,00,00,05,15,00,00,00,db,eb,0c,50,57,66,e2,62,82,8b,a6,28,77,04,00,\
  00,01,02,00,00,00,00,00,05,20,00,00,00,20,02,00,00,01,05,00,00,00,00,00,05,\
  15,00,00,00,db,eb,0c,50,57,66,e2,62,82,8b,a6,28,01,02,00,00
"0120052$"=hex:01,00,04,80,ac,00,00,00,bc,00,00,00,00,00,00,00,14,00,00,00,02,\
  00,98,00,04,00,00,00,00,00,24,00,ff,01,1f,00,01,05,00,00,00,00,00,05,15,00,\
  00,00,db,eb,0c,50,57,66,e2,62,82,8b,a6,28,e5,04,00,00,00,00,24,00,ff,01,1f,\
  00,01,05,00,00,00,00,00,05,15,00,00,00,79,e3,fc,53,44,dd,b8,3d,23,5f,63,6b,\
  0e,05,00,00,00,00,24,00,ff,01,1f,00,01,05,00,00,00,00,00,05,15,00,00,00,79,\
  e3,fc,53,44,dd,b8,3d,23,5f,63,6b,0b,05,00,00,00,00,24,00,ff,01,1f,00,01,05,\
  00,00,00,00,00,05,15,00,00,00,db,eb,0c,50,57,66,e2,62,82,8b,a6,28,77,04,00,\
  00,01,02,00,00,00,00,00,05,20,00,00,00,20,02,00,00,01,05,00,00,00,00,00,05,\
  15,00,00,00,db,eb,0c,50,57,66,e2,62,82,8b,a6,28,01,02,00,00
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Patrick MatthewsCommented:
rwhittle,

Using that sample data, just change one line in GrabData:

    Entries = RegExpFind(WholeFile, "(^|"")\d+\$""")


Regards,

Patrick
0
 
rwhittleAuthor Commented:
I copied the code change the line run it using Visual Basic 6 it runs but nothing happens.  I am expecting the program to ask me or read the data file then extract the data to the  result file.  Await your response
0
 
Patrick MatthewsCommented:
Can you upload a sample file to www.ee-stuff.com?
0
 
Patrick MatthewsCommented:
Dan,

In my testing, my code successfully extracted the targeted strings.  In my opinion, I answered the
question.

Thank you for your cleanup efforts,

Patrick
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.