B4J Question parsing with REGEX or other?

refsmmat

Member
Licensed User
Longtime User
Hi,

I am looking to parse blocks of text (I have a sample attached).
Specifically I want to extract the data between $$SOE and $$EOE, then take this data and split it up by the commas. I was planning to use regex.split to do both parts of this.

I have tried the first stage, splitting the data up with a split pattern of "$$", but this hasn't worked.

B4X:
    Dim text As String
text = "Unwanted text here including CRLF etc $$SOE 2457445.638888889, A.D. 2016-Feb-27 03:20:00.0000, -3.434088981408140E+00, -9.406082070997213E+00,  3.002006542448317E-01,  4.935555086865448E-03, -1.935056934039580E-03, -1.632165803143835E-04,  5.785831922971549E-02,  1.001785743017802E+01,  1.201026543732353E-04, $$EOE unwanted text after here and more CRLF "


data2 = Regex.Split("$$",text)
' Im looking to extract all of the values between $$SOE  and $$EOE, split by commas into a string array
Dim l As List
l.Initialize2(data2)
Log(l)
End Sub


I welcome any advice on my method or suggestions of better ways to do this. Ive attached a sample of the full data I am parsing.
Many thanks
Stephen
 

Attachments

  • horizons.txt
    6.3 KB · Views: 321

derez

Expert
Licensed User
Longtime User
Data2 should be declared as string(), then the first part will be data2(0), the central part is data2(1) and the last part will be data2(2).
 
Upvote 0

refsmmat

Member
Licensed User
Longtime User
Data2 should be declared as string(), then the first part will be data2(0), the central part is data2(1) and the last part will be data2(2).
Sorry - I had declared data as a global ( Dim data2() As String) & I didnt include this in the post.
The code I have produces only one result data2(0) & it is the full string.
full code below.


B4X:
Sub Process_Globals
   Private fx As JFX
   Private MainForm As Form
   Dim data2() As String
  
End Sub

Sub AppStart (Form1 As Form, Args() As String)
   MainForm = Form1
   'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
  
   Dim text As String
text = "Unwanted text here including CRLF etc $$SOE 2457445.638888889, A.D. 2016-Feb-27 03:20:00.0000, -3.434088981408140E+00, -9.406082070997213E+00,  3.002006542448317E-01,  4.935555086865448E-03, -1.935056934039580E-03, -1.632165803143835E-04,  5.785831922971549E-02,  1.001785743017802E+01,  1.201026543732353E-04, $$EOE unwanted text after here and more CRLF "


data2 = Regex.Split("$$",text)
' Im looking to extract all of the values between $$SOE  and $$EOE, split by commas into a string array
Log(data2(0))
Log(data2(1))  
  
Dim l As List
l.Initialize2(data2)
Log(l)
End Sub
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
$ is a special character to regex and needs to be escaped. Try using "\$\$" as the split string.
 
Upvote 0

rwblinn

Well-Known Member
Licensed User
Longtime User
Like this example:
B4X:
Sub AppStart (Form1 As Form, Args() As String)
   Dim text As String
   text = "Unwanted text here including CRLF etc $$SOE 2457445.638888889, A.D. 2016-Feb-27 03:20:00.0000, -3.434088981408140E+00, -9.406082070997213E+00,  3.002006542448317E-01,  4.935555086865448E-03, -1.935056934039580E-03, -1.632165803143835E-04,  5.785831922971549E-02,  1.001785743017802E+01,  1.201026543732353E-04, $$EOE unwanted text after here and more CRLF "
   
   Dim data2() As String = Regex.Split("\$\$",text)
   
   Log(data2(0))   'Unwanted text here including CRLF etc
   Log(data2(1))   'SOE 2457445.638888889, A.D. 2016-Feb-27 03:20:00.0000, -3.434088981408140E+00, -9.406082070997213E+00,  3.002006542448317E-01,  4.935555086865448E-03, -1.935056934039580E-03, -1.632165803143835E-04,  5.785831922971549E-02,  1.001785743017802E+01,  1.201026543732353E-04,
   Log(data2(2))   'EOE unwanted text after here And more CRLF

   Log("***")
   Dim data() As String = Regex.Split(",", data2(1))
   For Each s As String In data
     Log(s)
   Next
End Sub
 
Upvote 0

refsmmat

Member
Licensed User
Longtime User
Thanks Stevel05 and Rwblinn,
The suggestion worked as does the code example. I will follow up this post with the full code once the rest of it is working.
The code sources planet/asteroid coordinates fro JPL Horizons.
Regards
Stephen
 
Upvote 0
Top