You haven't specified what you want to use the URL for. URLs can be extremely complex, and finding a RegEx-based solution that always works correctly is a significant challenge. However, you could use 'Inline Java' to leverage Android/Java's standard `java.net.URI` class. Using AI, I created a B4J inline Java Proof of Concept (POC) capable of verifying the validity of URLs pointing to various images and documents. Given Java's large user base, I believe there is a lower risk of receiving incorrect test results, in addition to a lighter load on system resources.
AI report how it Works Under the Hood
- URI(urlString): The native Java environment breaks the string down into its official structural components (scheme, host, path, etc.). If characters are out of place or illegal, it throws an error immediately.
- uri.getScheme(): Isolates the prefix. This prevents things like ftp:// or file links from slipping through if you only want standard web traffic.
- uri.getHost(): Ensures that a root server location or domain name actually exists in the string (e.g., prevents just https:// from being marked as valid).
Addition:
Given the link you provided as an example, I assume you want to download a PDF file to test with the POC. I deliberately used a download from PortableApps for my testing to demonstrate why certain approaches work or don't work. Below, you will find details of the tests performed and explanations of the results, helping you choose the best solution for your program. Without a brief code example, no one can tell you whether this inline Java or your Regex formula will work.
The result for the correctness of the url 'http..://../anything' = false
Reason: It is http:// or https:// no spces
The result for the correctness of the url 'http://../anything' = false
Reason: 'http://../anything' has missing hostname
The result for the correctness of the url '
https://example.com/anything' = true
The result for matches allowed image extensions of web URL:
https://example.com/anything = false
The result for allowed Docs document extensions for web URL:
https://example.com/anything =
false
Reason: Valid url, however no valid download link on second line
Reason: exe is not given as a valid extension.
Fix: Add on line 133 the extension "exe"
Dim AllowedDocs() As String = Array As String("pdf", "html", "htm")
To or add the extension you want to download like 'zip'
Dim AllowedDocs() As String = Array As String("pdf", "html", "htm", "exe")
Reason: The difference between the two links lies in their function: one is a landing page URL that initiates a download script, while the other is a direct link to the static file on a download server