2

我似乎进入了一个非常复杂的领域(无论如何对我来说)。假设我有以下行:

1:11:39 "LOGIN ATTEMPT: "47576966" Arlond"

我想做的是分开时间(1:11:39)ID(47576966)和名称(Arlond)。我已经达到了下面的正则表达式,但我对接下来需要做的事情有点迷茫。我知道我的正则表达式不正确,无法获取我需要的一切,这也是我需要帮助以使我的 For 循环正常工作的地方。我一直在寻找如何正则表达式拆分和替换,但到目前为止,我还没有任何运气可以工作。

([""'])(?:(?=(\\?))\2.)*?\1


Using TestFile As New IO.StreamReader(My.Settings.cfgPath & "tempRPT.txt", System.Text.Encoding.Default, False, 4096)
        Do Until TestFile.EndOfStream
            ScriptLine = TestFile.ReadLine
            ScriptLine = LCase(ScriptLine)
            If InStr(ScriptLine, "login attempt:") Then
                Dim m As MatchCollection = Regex.Matches(ScriptLine, "([""'])(?:(?=(\\?))\2.)*?\1")
                For Each x As Match In m

                Next
                'builder.AppendLine(ScriptLine)
            End If

        Loop
    End Using
4

2 回答 2

1

关于您的正则表达式,我一直认为最好尽可能明确(例如锚点)。假设您的输入数据的表现与看起来一样好,您可以执行以下操作:

^(\d{1,2}:\d{2}:\d{2})\s""LOGIN\sATTEMPT:\s""(\d+)""\s([^""]+)""$

将其分解为其组件:

^                       // Anchor: Start of string (or line).
(\d{1,2}:\d{2}:\d{2})   // Capture one or two digits, colon, two digits, colon, two digits.
\s""LOGIN\sATTEMPT:\s"" // Anchor: match (but don't capture) literal text.
(\d+)                   // Match/capture one or more digits. (maybe you could use \d{8} instead).
""\s                    // Anchor: literal text.
([^""]+)                // Match and capture everything that is not a quote.
""                      // Anchor: Literal quote.
$                       // Anchor: End of string (or line).

name如果您的字段被允许包含"(双引号)字符,这将被打破。如果事实证明是这种情况,您将不得不修改最后一个子表达式以使其更宽松。

于 2012-07-30T19:03:01.650 回答
1

在回答 DavidO 接受的问题时,我只是想表明我已将其分解以更好地理解它。

If InStr(ScriptLine, "login attempt:") Then
                Dim m As Match = Regex.Match(ScriptLine, ("(\d{1,2}:\d{2}:\d{2})"))
                hurrburr = m.Value
                'Regex.Replace(ScriptLine, "(\d{1,2}:\d{2}:\d{2})", "")
                Dim mm As Match = Regex.Match(ScriptLine, "(\d{7,8})")
                'ScriptLine = ScriptLine & " " & mm.Value
                hurrburr = hurrburr & " " & mm.Value
                Dim mmm As Match = Regex.Match(ScriptLine, """\s([^""]+)")
                temp = mmm.Value.Replace("""", "")
                hurrburr = hurrburr & " " & temp
                builder.AppendLine(hurrburr)
 End If
于 2012-07-30T19:39:21.453 回答