Android Question Best simple parsing using Regex or maybe it's not the best way

imbault

Well-Known Member
Licensed User
Longtime User
Hi,

I'm looking the good regex way to parse (after spending 2 hours on it, I'm ashamed): txry where x and y can be any decimal values like

t1r3 : should retrieve 1 and 3
or
t11r61 : should retrieve 11 and 61

Else : should retrieve nothing or 0 and 0...

Thanks a lots

Patrick
 

roumei

Active Member
Licensed User
Probably not the most elegant way to do it but it works:
B4X:
Test("t1r3")
Test("t11r61")

Private Sub Test(s As String)    
    Dim n1, n2 As Int
    s = s.Replace("t", "")
    s = s.Replace("r", " ")
    Dim arr() As String = Regex.Split(" ", s)
    If arr <> Null And arr.Length = 2 And IsNumber(arr(0)) And IsNumber(arr(1)) Then
        n1 = arr(0)            
        n2 = arr(1)
    End If
    Log(n1 & " " & n2)
End Sub
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
Or, for this particular format...

B4X:
    Dim res() As String = Array As String(s.substring2(1, s.IndexOf("r")), s.substring(s.IndexOf("r") + 1))
    Log(res(0) & TAB & res(1))
 
Upvote 0
Top