Tell me, is there a regular expression to determine whether a text is a link in the most general form “http://” plus the characters allowed in the address?
Dim m As Matcher = Regex.Matcher(pat, str)
If m.Find() Then
Log("URL is valid")
Else
Log("URL is not valid")
End If
^http:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,}(:[0-9]+)?(\/[^\s]*)?$
^https?:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,}(:[0-9]+)?(\/[^\s]*)?$
Thank you! I will test it at different addresses.^https?:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,}[0-9]+)?(\/[^\s]*)?$
Dim pat, str As String
pat="^https?:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,}(:[0-9]+)?(\/[^\s]*)?$"
str="https://www.b4x.com/android/forum/forums/android-questions.26/"
Dim m As Matcher = Regex.Matcher(pat,str)
I understand this is already flagged as solved, but why not let Java/Android do the heavy lifting of checking if a URL is compliant?is there a regular expression to determine whether a text is a link in the most general form “http://” plus the characters allowed in the address
'https://www.baeldung.com/java-validate-url
Public Sub isValidURL(URL As String) As Boolean
Dim success As Boolean = True
Try
Dim joURL As JavaObject
joURL.InitializeNewInstance("java.net.URL", Array(URL))
joURL.RunMethod("toURI", Null)
Catch
success = False
'Log(LastException)
End Try
Return success
End Sub
Public Sub isValidHttpURL(URL As String) As Boolean
Dim success As Boolean
Try
If URL.ToLowerCase.StartsWith("http") Then
success = isValidURL(URL)
End If
Catch
Log(LastException)
End Try
Return success
End Sub
If you do as you advise, then it’s better like this:Looks like I'm shooting myself in the foot
If URL.ToLowerCase.StartsWith("https://") Or URL.ToLowerCase.StartsWith("http://") Then
Yes, that would future-proof it in case java.net.Url supports some weird scheme that starts with HTTP, but is not HTTP or HTTPS. In reality, my solution may not be that good after all, since it would allow for some weird standard's compliant addresses that the OP (you - @Sergey_New ) may not want as valid:then it’s better like this:
(see https://stackoverflow.com/a/5965755)
I need to check the syntax of the HTTP address as it is entered by the user, so that there are no invalid characters.As another side note