解析由空格分隔的日志文件

时间:2014-11-17 20:31:58

标签: vb.net

我正在尝试解析由空格分隔的VB中的apache access.log文件,但其中一个字段由"" 我试过了

Using ioReader As New Microsoft.VisualBasic.FileIO.TextFieldParser(foundFile)
ioReader.TextFieldType = FileIO.FieldType.Delimited
ioReader.SetDelimiters(" ")    ioReader.HasFieldsEnclosedInQuotes = True
Dim currentRow As String()
While Not ioReader.EndOfData
End While
End Using

但是我收到错误:无法使用当前分隔符解析第1行。 如果我更改HasFieldsEnclosedInQuotes = False,那么我的数据格式不正确

我想要阅读的示例行是:

192.168.0.1 - - [17/Nov/2014:03:34:29 +0000] "GET /bluebell.html HTTP/1.1 "200 13675 "-" "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)" "-"[RT:0.769] [C:1115486]

1 个答案:

答案 0 :(得分:1)

有几种方法可以解决这个问题。一种方法是使用Try / Catch来忽略或修复任何格式不正确的行。另一种方法是将每一行作为字符串读取,并使用正则表达式或字符串操作在程序中自行解析。