0

我有一个日志文件,我想在其中获取以 . 开头<fullpath>"和结尾的子字符串"。例如:

会员 ID=":LtWeek2:1133548" fullPath="Time.Latest Weekly : FOLDER.Latest 52 Weeks Ending 04-21-13.Week Ending 02-24-13" levelName="LtWeek2" rpmSetOperator="Add"/会员 ID =":LtWeek2:1168692" fullPath="Time.Latest Weekly : FOLDER.Latest 52 周结束 04-21-13.Week Ending 02-24-13" levelName="LtWeek2" rpmSetOperator="Add"/Member ID=" :LtWeek2:1191834" fullPath="Time.Latest Weekly : FOLDER.Latest 52 周结束 04-21-13.Week Ending 03-10-13" levelName="LtWeek2" rpmSetOperator="Add"

比如说,上述数据将出现在“A1”中。

现在我必须编写一个代码,使得只有字符串,如

fullPath="Time.Latest Weekly : FOLDER.Latest 52 Weeks Ending 04-21-13.Week Ending 02-24-13" fullPath="Time.Latest Weekly : FOLDER.Latest 52 Weeks Ending 04-21-13.Week Ending 02-24-13" fullPath="Time.Latest Weekly : FOLDER.Latest 52 周结束 04-21-13.Week Ending 03-10-13"

所以我的想法是,以“fullPath”作为起始词,以“levelName”作为结束词,如果我们能够获得介于两者之间的文本,我的问题就可以解决。

以上方法只是从我这边,任何其他方法/方法也可以。

4

1 回答 1

0

以下代码读取 Sheet1 的单元格 A1 中的字符串,提取感兴趣的子字符串,并将它们写回工作表的 B、C 和 D 列中。

  Sub ExtractFromLog()
      Dim str As String
      Dim entries() As String
      Dim numEntries As Long
      Dim i As Long
      Dim startPos As Long
      Dim endPos As Long

      str = Sheet1.Range("A1").Value
      numEntries = (Len(str) - Len(Replace(str, "fullPath", ""))) / Len("fullPath")
      ReDim entries(1 To numEntries)

      startPos = 1
      For i = 1 To numEntries
          startPos = InStr(startPos, str, "fullPath")
          endPos = InStr(startPos + 1, str, "levelName") - 1
          entries(i) = Mid(str, startPos, endPos - startPos + 1)
          startPos = endPos + 1
      Next i
      Sheet1.Range(Cells(1, 2), Cells(1, i)) = entries
  End Sub
于 2013-06-14T04:42:17.513 回答