0

I have a long string. Within this string, there are strings I want to parse. After parsing, I pass the values to a listview. It´s already working, but I have struggles with one String.. Here is a picture:

Here you can see, that the second value is wrong

I am using this string:

UserToken:8VE91632C25166906Amount:5.00Accounts:10buyTime:2018-02-20 12:16:56untilTime:2018-03-22 12:16:56EndBREAKUserToken:2BB32950CL297560CAmount:25.00Accounts:50buyTime:2018-02-21 13:05:52untilTime:2018-03-23 13:05:52EndBREAKUserToken:8S034548J4871372YAmount:30.00Accounts:60buyTime:2018-02-21 15:26:28untilTime:2018-03-23 15:26:28EndBREAKUserToken:84692313143307443Amount:60.00Accounts:120buyTime:2018-02-22 11:33:54untilTime:2018-03-24 11:33:54EndBREAKUserToken:3JJ04496CB952290AAmount:30.00Accounts:60buyTime:2018-02-23 19:28:42untilTime:2018-03-25 19:28:42EndBREAKUserToken:9K197884LF5914344Amount:60.00Accounts:120buyTime:2018-02-27 17:07:16untilTime:2018-03-29 17:07:16EndBREAKUserToken:28C99011N17519701Amount:135.00Accounts:180buyTime:2018-03-05 09:00:00untilTime:2018-04-05 09:00:00EndBREAKUserToken:0TD98762R1733752EAmount:225.00Accounts:300buyTime:2018-03-07 19:00:00untilTime:2018-04-07 20:00:00EndBREAK

This is the way how I parse the values:

 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

The result of the second string I parse is always "7560C", so it´s only a part of the string.. The other values are correct. Also, when I set this string to another string like "8VE91632C25166906" its showing me the whole String. Also with regex i can´t get the full string.. Where is the problem? This drives me crazy..

Best regards..

xored
  • 1
  • Splitting on a character array does not search for a string. It splits on **all** of the characters inside the array. – Nyerguds Mar 08 '18 at 18:48
  • Possible duplicate of [C# Split A String By Another String](https://stackoverflow.com/questions/2245442/c-sharp-split-a-string-by-another-string) – Nyerguds Mar 08 '18 at 18:53

2 Answers2

1

The following expression below will match all instances in the string you've specified:

Expression:

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

Code (auto-generated, you might want to adjust it):

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

Check the expression online (click the Table tab)

aybe
  • 15,516
  • 9
  • 57
  • 105
  • Welcome to SO, note, when you are satisfied by answer, accept it (check the mark on its left) so it can be seen as answered on the site. – aybe Mar 08 '18 at 23:34
0

You are looking for something between Token and Amount:.
So the regex
(?<=Token\:)(.*?)(?=Amount\:)
will match what you're looking for.
Explanation:
(?<=Token\:): it's a look behind, so that the expression you write after this will match only if 'Token:' is present right before the expression after it.
(.*?): matches every character except by the new line character. So, you'll be able to match anything, how many times you need till it founds something you wrote after (.*?) (i.e the match will stop when the string 'Amount:' if found ahead of the current match).
(?=Amount\:): it's a look ahead. Works in exactly the same way look behind works, except that it seeks for 'Amount' right after the previous matched expression.

I don't know how to help you with the other way you tried to solve the problem but hope this satisfies you once you said you've tried regex (and therefore I infer you are willing to use them :) ).

Leonardo Maffei
  • 352
  • 2
  • 6
  • 16
  • Hey, working like a charm, but when I try to change the regex to: (?<=Amount\:)(.*?)(?=Accounts\:) I get "An item with the same key has already been added" exception. So, how to get the other values? :) – xored Mar 08 '18 at 19:26
  • 1 - @xored please upvote my answer, if it works. 2 - you can use the same reasoning if you understand correctly the concepts I explained. 3 - I don't know why this is happening. The regex you typed will catch everything but a newline between 'Amount:' and 'Accounts:' – Leonardo Maffei Mar 08 '18 at 23:06