B4J Question String parse

paddy12309

Member
Licensed User
Hi Everyone!

Gotten stuck trying to receive some data from a post request and put it into a database, I get data like this; 'PIRTIMEOUT 001306894=5000'

I need to put the 5000 (**** in the code)part into the database the other number being the device number (Num, in the code) it has to go against, but having removed the '00'.

B4X:
'Handler class
Sub Class_Globals
    Private mreq As ServletRequest 'ignore
    Private mresp As ServletResponse 'ignore
End Sub

Public Sub Initialize
     
End Sub

Sub CheckSN
    Dim Num As Int
        If 
End Sub

Sub Handle(req As ServletRequest, resp As ServletResponse)
    mreq = req
    Log(req)
    Dim params As TextReader
    params.Initialize(mreq.InputStream)
    Log(params.ReadAll)
'    Dim text2 As HttpJob
'        text2.GetString
'        Log(text2)
       
    Dim in2 As String
    in2 = params.ReadAll
    'parse string to get data;****, and DeviceSN; Num
    If in2.Contains(Num) Then
       
        Dim ParamsSetter As String
        ParamsSetter = "UPDATE EnergyHours(DeviceSN, StandbyLevel) VALUES ('Num', ****)"
    Else
        Log("Error Writing to database" & Num)                                   
    End If
       
End Sub

This is where I've gotten to so far but am going round in circle trying to work it out.

Any helps great;y appreciated!!
 

emexes

Expert
Licensed User
Awesome Thankyou! how would i use that to work the other way to get the previous 6 deigits?
B4X:
Dim SamplePost As String = "?00153067=1&00153067=198;00153067=123&4439=023"

Log(SamplePost)

Dim m As Matcher = Regex.Matcher("0*(\d+)=(\d+)", SamplePost)

Do While m.Find
    If m.GroupCount = 2 Then
        Dim DeviceSN As String = m.Group(1)
        Dim StandbyLevel As String = m.Group(2)
        Log("DeviceSN " & DeviceSN & " has StandbyLevel " & StandbyLevel)
    Else
        Log("wtf " & m.Group(0))    'wtf = weird text format ;-)
    End If
Loop
B4X:
Program started.
?00153067=1&00153067=198;00153067=123&4439=023
DeviceSN 153067 has StandbyLevel 1
DeviceSN 153067 has StandbyLevel 198
DeviceSN 153067 has StandbyLevel 123
DeviceSN 4439 has StandbyLevel 023
 
Upvote 0

emexes

Expert
Licensed User
You can get an (almost) understandable explanation of the regex at:

www.regex101.com

where the two sets of brackets () indicate that it is capturing two groups of numbers in the format 111=222, and that any leading zeroes before the first group are outside of the () and thus not included in the DeviceSN capture (as per your "having removed the '00'" indication).

upload_2019-8-23_16-35-24.png
 
Upvote 0
Top