Android Code Snippet [B4X] Check is URL

Alexander Stolte

Expert
Licensed User
Is not perfect, but most URLs should match it.
B4X:
Private Sub isURL(url As String) As Boolean
    Return Regex.IsMatch("(https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|www\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9]+\.[^\s]{2,}|www\.[a-zA-Z0-9]+\.[^\s]{2,})$",url)
End Sub
Example:
    If isURL("https://stackoverflow.com/a/17773849") = True Then
        Log("valid URL")
        Else
            Log("is not a URL")
    End If
 

JohnC

Well-Known Member
Licensed User
I am a regex syntax newbie, but all I see is two mentions of "https" in the pattern, but I don't see just an "http".

So I just was wondering if a url just had http and not https, will it pass the test?
 

Alexander Stolte

Expert
Licensed User
So I just was wondering if a url just had http and not https, will it pass the test?
click on the link and you will see the following:

Will match the following cases
Will NOT match the following
 
Top