0

I have a string which is a part of a text file. I need to extract data related to each item.
The string is:
GRID "G1 Global" LABEL "A 2 3" DIR "X-1" COORD 0 VISIBLE "Yes"
I used Split by "space" and "quotation marks" but the the resulats was not as I expected. I need to assign data to each item. For example "G1 Global" for GRID and 0 for COORD.
I used tis code for extract each word from the string:

    Dim linestring As Object
    Dim word0 As String
    Dim i As Integer
    Dim goalstring As String = TextBox2.Text 'Thextbox2.text = GRID "G1 Global"  LABEL "A 2 3"  DIR "X-1"  COORD 0 VISIBLE "Yes"
    'linestring = Split(goalstring, """")
    linestring = Split(goalstring, " ")
    For Each word0 In linestring
        If word0 <> "" Then
            i += 1
            Console.WriteLine(i & ":" & word0)
        End If
    Next

The expected result is:
1:GRID
2:G1 Global
3:LABEL
4:A 2 3
5:DIR
6:X-1
7:COORD
8:0
9:VISIBLE
10:"Yes"

But I get this by Split(goalstring, " "):
1:GRID
2:"G1
3:Global"
4:LABEL
5:"A
6:2
7:3"
8:DIR
9:"X-1"
10:COORD
11:0
12:VISIBLE
13:"Yes"

and this by Split(goalstring, """"):
1: GRID
2:G1 Global
3: LABEL
4:A 2 3
5: DIR
6:X-1
7: COORD 0 VISIBLE
8:Yes

kamyar
  • 41
  • 6
  • 1
    It's the same thing as if you were splitting a normal CSV file except you have space as the delimiter. I suggest you [look at how to properly parse delimited string in vb](https://stackoverflow.com/a/736647/130611). – the_lotus Jan 10 '19 at 16:04
  • You could use Regex from following answer (in C#): https://stackoverflow.com/questions/554013/regular-expression-to-split-on-spaces-unless-in-quotes – Meta-Knight Jan 10 '19 at 16:31
  • 1
    You can either select what you want with regex like the link Meta-Knight provided, or split it by what you don't want which is a space or anything enclosed in quotes like `Regex.Split(yourString, "(""[^""]*"")|\s")` – soohoonigan Jan 10 '19 at 16:41

1 Answers1

3

While it's absolutely fine to use regex expressions, personally I find the, obtuse and difficult to debug. I would rather write personal code - for example The code below iterates through each character of the string.

If it finds a character that isn't a space or a quote, it adds it to the word0.

If it finds a space, it writes word0 to the console.

If it finds a quote, it adds everything after the quote to word0 until it finds the next quote. It then writes word0 to the console.

Dim word0 As String = ""
Dim goalstring As String = TextBox2.Text
For i As Integer = 0 To goalstring.Length - 1
    Select Case goalstring(i)
        Case " "c
            Console.WriteLine(word0)
            word0 = ""
        Case """"c
            Do While goalstring(i + 1) <> """"
                i += 1
                word0 = word0 & goalstring(i)
            Loop
            Console.WriteLine(word0)
            i += 2
            word0 = ""
        Case Else
            word0 = word0 & goalstring(i)
    End Select
Next

If you want quotes around the last outputted line then you'll need to alter your code to add each word to a list instead of writing to the console. Then add quotes to the last item in the list and then write the list to the console.

David Wilson
  • 4,369
  • 3
  • 18
  • 31
  • Glad I'm not the only one that prefers to write relatively simple blocks of code over regex. Regex is usually fine when you're writing it, but if you have to go back and make adjustments to it some time later, you have to basically re-learn what you wrote. – user2366842 Jan 10 '19 at 23:31
  • Hehe - never mind even when someone else has to edit/debug it! :) – David Wilson Jan 10 '19 at 23:34