Getting multiple values from a String

2

Is there any way to get the arguments as in the example below, in a separate variable? I'm creating a language, and this is an example block:

for 0 to 250 step 50
^^^ ^ ^^ ^^^ ^^^^ ^^

Then I used the String.Split(" ") method and it worked, but the question is, and if you want to put a field that has spaces, for example:

if "esse campo tem espaços" = true
^^ ^^^^^^^^^^^^^^^^^^^^^^^^ ^ ^^^^

but it is returning like this:

if "esse campo tem espaços" = true
^^ ^^^^^ ^^^^^ ^^^ ^^^^^^^^ ^ ^^^^

Instead of getting into an array like this:

if
esse campo tem espaços
=
true

he is separating what is in the field too, so it looks like this:

if
esse
campo
tem 
espaços
=
true

and the conflict ... Is there any way to get the values inside the field without doing this?

    
asked by anonymous 24.05.2015 / 00:55

1 answer

2

You may not be able to use String.Split() For that purpose, regular expressions might fall well in this case, use the method Regex.Split() :

Imports System.Text.RegularExpressions
'

Function ExtrairBlocos(ByVal texto As String) As List(Of String)
    Dim blocos As New List(Of String)
    blocos = Regex.Split(texto, "(""[^""]*""|\s+)").ToList()
    blocos.RemoveAll(Function(bloco) String.IsNullOrWhiteSpace(bloco))
    Return blocos
End Function

And to use, do:

Sub Main()
    Dim texto As String = "if ""esse campo tem espaços"" = true"
    Dim blocos As New List(Of String)
    blocos = ExtrairBlocos(texto)

    For Each bloco As String In blocos
        Console.WriteLine("{0}", bloco)
    Next
    Console.ReadLine()
End Sub

View demonstração

    
24.05.2015 / 02:01