Android Question [SOLVED]regex and wildcards

giggetto71

Active Member
Licensed User
Longtime User
Hi,
I am trying to check if there is an easy way to use regex to verify if a received topic matches a wildcard subscription.
example:

subscribed: "+/room/+/light/+"
received "house1/room/bedside/light/status"


I don't have experience with Regex so I was wondering if someone can help with an example function that gets received string and subscribed string and return true if (like in this case the received matched the wildcard definition.
thanks a lot!
 
Last edited:

giggetto71

Active Member
Licensed User
Longtime User
ok thanks.
I have actually found the solution proposed by @madru (BTW THANKS!!!) in https://www.b4x.com/android/forum/threads/solved-regex-for-mqtt.80668/
I only made small modification in the "CreateMatch" sub leveraging the native strings methods.
B4X:
Sub CreateMatch(text As String) As String
    If text.Contains("+") Then
        Dim a As String = text.Replace("+","([\w\.\-\$]+)")
        a= a.Replace("\/","\\/")
    End If
    If a.Contains("#") Then
        Dim a As String = a.Replace("#","([\w\.\-\$]+(\/[\w\.\-\$]+)*)")
    End If
    Return a
End Sub
 
Upvote 0
Top