B4J Question How to split on a string by spaces, but don't parse the quotes?

B4JExplorer

Active Member
Licensed User
Longtime User
Hi,

I can do this in Java, but haven't worked it out in B4J, even using Chr(34) to replace the quote in the regex.

Does anyone have a ready-made example of a Regex that will work in B4J, to parse this:

Hello, there. "How are you doing, there?"


as

Hello,
there
How are you doing, there?
 

B4JExplorer

Active Member
Licensed User
Longtime User
I was just hoping that someone had already done something that was simpler than my java testing, which didn't work in B4J.

The pattern I used, was

Pattern.compile("([^\"]\\S*|\".+?\")\\s*")

In B4J, I had it as

s_RegEx = "([^\ & Chr(34) & ]\\S*|\ & Chr(34) & .+?\ & Chr(34) & )\\s*"


Anyway, for the time being I'm just letting all of it parse out to a list, but then re-collecting the relevant string.

B4X:
					Do While lst_Parameters.Get(n_Nth_Parameter) <> "0"
						If n_Nth_Parameter = 2 Then
							s_Query = lst_Parameters.Get(n_Nth_Parameter)
						Else
							s_Query = s_Query & " " & lst_Parameters.Get(n_Nth_Parameter)
						End If
						n_Nth_Parameter = n_Nth_Parameter + 1
					Loop
					n_Nth_Parameter = 0
					s_Query.Replace( " ", "" )


If anyone has an existing B4J pattern that will parse out spaced words but ignore quoted strings, it would be helpful but not necessary. Thanks.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Sometimes it's just easier not to use Regex. Try this:

B4X:
Dim Pos = 0 , Pos1 = 0, LastPos = 0 As Int
    Dim S As String = $"Hello, there. "How are you doing, there?""$
    Dim Result As List
    Result.Initialize
   
    'Make sure there are no double spaces.
    Do While S.Contains("  ")
        S = S.Replace("  "," ")
    Loop


    Pos = S.IndexOf2(" ",LastPos)
    Do While Pos > -1
       
        If S.CharAt(Pos + 1) = QUOTE Then
            Result.Add(S.SubString2(LastPos,Pos))
            Pos1 = S.IndexOf2(QUOTE,Pos + 2)
            If Pos1 = -1 Then
                Result.Add(S.SubString(Pos + 1))
                LastPos = S.Length
                Log("Error: Closing quote not found")
            Else
                Result.Add(S.SubString2(Pos + 2, Pos1))
                LastPos = Pos1 + 1
            End If
        Else
            Result.Add(S.SubString2(LastPos,Pos))
            LastPos = Pos + 1
        End If
        Pos = S.IndexOf2(" ",LastPos)
    Loop
   
    For Each S As String In Result
        Log(S)
    Next
   
    Log("Done")
 
Upvote 0

B4JExplorer

Active Member
Licensed User
Longtime User
Sometimes it's just easier not to use Regex. Try this:

B4X:
Dim Pos = 0 , Pos1 = 0, LastPos = 0 As Int
    Dim S As String = $"Hello, there. "How are you doing, there?""$
    Dim Result As List
    Result.Initialize
   
    'Make sure there are no double spaces.
    Do While S.Contains("  ")
        S = S.Replace("  "," ")
    Loop


    Pos = S.IndexOf2(" ",LastPos)
    Do While Pos > -1
       
        If S.CharAt(Pos + 1) = QUOTE Then
            Result.Add(S.SubString2(LastPos,Pos))
            Pos1 = S.IndexOf2(QUOTE,Pos + 2)
            If Pos1 = -1 Then
                Result.Add(S.SubString(Pos + 1))
                LastPos = S.Length
                Log("Error: Closing quote not found")
            Else
                Result.Add(S.SubString2(Pos + 2, Pos1))
                LastPos = Pos1 + 1
            End If
        Else
            Result.Add(S.SubString2(LastPos,Pos))
            LastPos = Pos + 1
        End If
        Pos = S.IndexOf2(" ",LastPos)
    Loop
   
    For Each S As String In Result
        Log(S)
    Next
   
    Log("Done")
Yep, that's basically what I've been doing. I'll go ahead and test this routine tomorrow, thanks Steve.
 
Upvote 0
Top