简体   繁体   中英

extracting from a string without delimiters excel

This site has been a veritable treasure chest of answers and ideas to many of my vba problems in the past, but i have not been able to find any concerning what i am sure is for many, if not most, here in this forum a simple task. I have to deal with a lot of xml report files that all have a header string and my problem is how to parse the string for the nuggest i require for my macro. This is a sample string:

<Function IDREF="TST_RxRccsMatrix_Rx64" Start="2011-04-07T14:21:35.593000+02:00" Status="Success" Tags="SystemSerialNumber:41009" End="2011-04-07T14:29:16.625000+02:00">

I need to extract - the report type: TST_RxRccsMatrix (length of this string is not constant) - the start date-time stamp: 2011-04-07T14:21:35.593000+02:00 (length is constant) - the serial number: 41009 (length is constant)

I have tried methods using Split and InStr and Find but none produce the desired results for all three extractions.

I truely appreciate any help on this!

The old fashion way is to use instr to find beginning. Then use instr to find ending. Then use mid to suck it out.

Begin = instr(1,xmlstring,"IDREF=") + Len("IDREF")
'look for first space after IDREF= in string
End = instr(Begin, xmlstring, " ") 
Report = mid(xmlstring, begin, end - begin)

I didn't test it.

But I's split on space, then go through the array splitting on =. That will give you an array of 2 element arrays with value name in (0) and value in (1).

But xml has it's own query language and libraries to access stuff.

This is some code splitting a command line and then splitting 320x200 into 300 and 200.

CmdLine = Command()

A = Split(CmdLine, Chr(32), 2, 1)
B = Split(A(0), "x", 2, 1) 

After some polishing:

Private Sub GetFileInfo()
 Dim fso As New FileSystemObject, strText As Variant, i As Integer
Dim X(0 To 2) As String, Y(0 To 2) As String, B, E As Variant
'get header string from xml file
'FName (file name) was ascertained by a previous sub and is made public
Set strText = fso.OpenTextFile(FName, ForReading, False)
'header string is in second (i = 2) line of file
For i = 1 To 2: [A1] = strText.ReadLine: Next: strText.Close: Set fso = Nothing
'User Oded's search and extract routine
X(0) = "IDREF=": X(1) = "Start=": X(2) = "Tags="
For i = LBound(X(), 1) To UBound(X(), 1)
  B = InStr(1, [A1], X(i)) + Len(X(i)) + 1 ' + 1 includes trailing " character
  E = InStr(B, [A1], " ") - 1 ' - 1 includes leading " character
'required if a search string in X() is at the end of the header which ends with a ">"
  If (InStr(B, [A1], " ") - 1) < 0 Then E = InStr(B, [A1], ">")
  Y(i) = Mid([A1], B, E - B)
Next
[D1] = "Test = " & Y(0)
[D2] = "Tested on : " & Left(Y(1), 10) & " at " & Mid(Y(1), 12, 8)
[D2] = [D2] & " - " & Y(2)
End Sub
xmlstring = "<Function IDREF=""TST_RxRccsMatrix_Rx64"" Start=""2011-04-07T14:21:35.593000+02:00"" Status=""Success"" Tags=""SystemSerialNumber:41009"" End=""2011-04-07T14:29:16.625000+02:00"">"
Set regEx = New RegExp
regEx.Pattern = "IDREF=""([a-z0-9_]+)"""
regEx.IgnoreCase = True
regEx.Global = True
Set Matches = regEx.Execute(xmlstring)
If Matches.count <> 1 then msgbox "no match or too many"
For Each Match in Matches
    Msgbox match.submatches(0)
Next

I answered your qustions. The other person deleted two easier ways of doing it.

Ask Oded to put back my explanation of this code. And to restore the MS tutorial on how to do it with XML DOM objects. I showed FOUR ways.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM