Regex VB.net获取两个字符串之间的字符串返回错误或错误值

Regex VB.net获取两个字符串之间的字符串返回错误或错误值,regex,vb.net,Regex,Vb.net,我有一根长长的绳子。在这个字符串中,有我想要解析的字符串。 解析之后,我将值传递给listview。 它已经开始工作了,但我一直在为一根弦而挣扎。。 这是一张照片: 我正在使用这个字符串: UserToken:8VE91632C2516906装载量:5.00帐户:10购买时间:2018-02-20 12:16:56截止时间:2018-03-22 12:16:56 EndBreakUserToken:2BB32950CL297560CAmount:25.00账户:50购买时间:2018-02-2

我有一根长长的绳子。在这个字符串中,有我想要解析的字符串。 解析之后,我将值传递给listview。 它已经开始工作了,但我一直在为一根弦而挣扎。。 这是一张照片:

我正在使用这个字符串:

UserToken:8VE91632C2516906装载量:5.00帐户:10购买时间:2018-02-20 12:16:56截止时间:2018-03-22 12:16:56 EndBreakUserToken:2BB32950CL297560CAmount:25.00账户:50购买时间:2018-02-21 13:05:52截止时间:2018-03-23 13:05:52 EndBreakUserToken:8S034548J4871372 Yamount:30.00账户:60购买时间:2018-02-21 15:26:28截止时间:2018-03-23 15:26:28EndBREAKUserToken:84692313143307443装载:60.00帐户:120购买时间:2018-02-22 11:33:54截止时间:2018-03-24 11:33:54 EndBreakUserToken:3JJ04496CB952290A装载量:30.00帐户:60购买时间:2018-02-23 19:28:42截止时间:2018-03-25 19:28:42 EndBreakUserToken:9K197884LF5914344金额:60.00账户:120购买时间:2018-02-27 17:07:16截止时间:2018-03-29 17:07:16 EndBreakUserToken:28C99011N17519701金额:135.00账户:180购买时间:2018-03-05 09:00:00截止时间:2018-04-05 09:00:00EndBREAKUserToken:0TD98762R1733752EAmount:225.00账户:300购买时间:2018-03-07 19:00:00截止时间:2018-04-07 20:00:00结束中断

这就是我解析值的方式:

 Dim words() As String
        Dim space() As Char = {"BREAK"}
        words = STRINGABOVE.Split(space)
        Dim word As String


        For Each word In words


            Try
                Dim sSource As String = word
                Dim sDelimStart As String = "UserToken:"
                Dim sDelimEnd As String = "Amount:"
                Dim nIndexStart As Integer = sSource.IndexOf(sDelimStart)
                Dim nIndexEnd As Integer = sSource.IndexOf(sDelimEnd, nIndexStart + sDelimStart.Length + 1)

                Dim sDelimStart2 As String = "Amount:"
                Dim sDelimEnd2 As String = "Accounts:"
                Dim nIndexStart2 As Integer = sSource.IndexOf(sDelimStart2)
                Dim nIndexEnd2 As Integer = sSource.IndexOf(sDelimEnd2, nIndexStart2 + sDelimStart2.Length + 1)

                Dim sDelimStart3 As String = "Accounts:"
                Dim sDelimEnd3 As String = "buyTime:"
                Dim nIndexStart3 As Integer = sSource.IndexOf(sDelimStart3)
                Dim nIndexEnd3 As Integer = sSource.IndexOf(sDelimEnd3, nIndexStart3 + sDelimStart3.Length + 1)

                Dim sDelimStart4 As String = "buyTime:"
                Dim sDelimEnd4 As String = "untilTime:"
                Dim nIndexStart4 As Integer = sSource.IndexOf(sDelimStart4)
                Dim nIndexEnd4 As Integer = sSource.IndexOf(sDelimEnd4, nIndexStart4 + sDelimStart4.Length + 1)

                Dim sDelimStart5 As String = "untilTime:"
                Dim sDelimEnd5 As String = "End"
                Dim nIndexStart5 As Integer = sSource.IndexOf(sDelimStart5)
                Dim nIndexEnd5 As Integer = sSource.IndexOf(sDelimEnd5, nIndexStart5 + sDelimStart5.Length + 1)

                Dim res As String = Strings.Mid(sSource, nIndexStart + sDelimStart.Length + 1, nIndexEnd - nIndexStart - sDelimStart.Length)
                Dim res2 As String = Strings.Mid(sSource, nIndexStart2 + sDelimStart2.Length + 1, nIndexEnd2 - nIndexStart2 - sDelimStart2.Length)
                Dim res3 As String = Strings.Mid(sSource, nIndexStart3 + sDelimStart3.Length + 1, nIndexEnd3 - nIndexStart3 - sDelimStart3.Length)
                Dim res4 As String = Strings.Mid(sSource, nIndexStart4 + sDelimStart4.Length + 1, nIndexEnd4 - nIndexStart4 - sDelimStart4.Length)
                Dim res5 As String = Strings.Mid(sSource, nIndexStart5 + sDelimStart5.Length + 1, nIndexEnd5 - nIndexStart5 - sDelimStart5.Length)

                ListView1.Items.Add(New ListViewItem({res, res2, res3, res4, res5}))

            Catch
            End Try
        Next
我解析的第二个字符串的结果总是“7560C”,因此它只是字符串的一部分。。其他值是正确的。此外,当我将此字符串设置为另一个字符串(如“8VE91632C2516906”)时,它会显示整个字符串。同样使用正则表达式,我无法获得完整字符串。。问题在哪里?这让我发疯

致以最诚挚的问候。

您正在寻找介于
代币
金额:

所以正则表达式

(?以下表达式将匹配指定字符串中的所有实例:

表达方式:

UserToken:([A-Z0-9]{17})Amount:(\d+\.\d+)Accounts:(\d+)buyTime:(\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2})untilTime:(\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2})EndBREAK
代码(自动生成,您可能需要对其进行调整):


在字符数组上拆分不会搜索字符串。它会在数组中的所有字符上拆分。Hey可能重复,工作起来很有魅力,但当我尝试将正则表达式更改为:(?1-@xored请更新我的答案,如果它有效。2-如果你正确理解了我解释的概念,你可以使用相同的推理。3-我不知道为什么会发生这种情况。你键入的正则表达式将捕捉所有内容,除了“金额:”和“帐户:”之间的换行符:欢迎这样做,注意,当你对答案感到满意时,接受它(勾选左边的标记),以便在现场看到答案。
Dim AllMatchResults As MatchCollection
Try
    Dim RegexObj As New Regex("UserToken:([A-Z0-9]{17})Amount:(\d+\.\d+)Accounts:(\d+)buyTime:(\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2})untilTime:(\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2})EndBREAK")
    AllMatchResults = RegexObj.Matches(SubjectString)
    If AllMatchResults.Count > 0 Then
        ' Access individual matches using AllMatchResults.Item[]
    Else
        ' Match attempt failed
    End If
Catch ex As ArgumentException
    'Syntax error in the regular expression
End Try