Message Boards Message Boards

2 Replies
9 Total Likes
View groups...
Share this post:

Test if expression is a URL - URLQ?

Posted 8 years ago

Mathematica has a number of functions to test if expressions represent a type of object. A summary is provided in the Help section guide/TestingExpressions. Unfortunately, there does not seem to be a "URLQ" to test if an expression is a string containing an URL. I could probably come up with some sloppy heuristics to write my own, but I was wondering if there was a best practice for testing if an expression contains a URL. Please let me know if you have any suggestions.

2 Replies
Posted 8 years ago

There is a regex to test whether a string represents a correct URL according to RFC 3986, which is a part of commercial RegexBuddy Library. It is quite complicated though, so if you don't need a strong verification you can use one of the solutions from the following SO thread:

POSTED BY: Alexey Popkov

The problem is that what can be considered a URL actually varies depending on context. And sometimes something that is not a complete URL can be promoted to one.

I suggest you look up Interpreter["URL"] and its restrictions. Then you'll probably be able to write something like:

URLQ[s_] := !FailureQ[Interpreter["URL"][s]]
POSTED BY: Carlo Barbieri
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract