What you're looking for is Uri.IsWellFormedUriString
. The following code returns true:
Uri.IsWellFormedUriString("google.com", UriKind.RelativeOrAbsolute)
If you set UriKind to Absolute, it returns false:
Uri.IsWellFormedUriString("google.com", UriKind.Absolute)
EDIT:
See here for UriKind enumeration.
- RelativeOrAbsolute: The kind of the Uri is indeterminate.
- Absolute: The Uri is an absolute Uri.
- Relative: The Uri is a relative Uri.
From MSDN documentation:
Absolute URIs are characterized by a complete reference to the resource (example: http://www.contoso.com/index.html), while a relative Uri depends on a previously defined base URI (example: /index.html).
Also, see here for Uri.IsWellFormedUriString
. This method works in accordance with RFC 2396 and RFC 2732.
If you look at RFC 2396, you'll see that google.com is not a valid URI. In fact www.google.com isn't neither. But under F. Abbreviated URLs, this situtation is explained in detail as follows:
The URL syntax was designed for unambiguous reference to network
resources and extensibility via the URL scheme. However, as URL
identification and usage have become commonplace, traditional media
(television, radio, newspapers, billboards, etc.) have increasingly
used abbreviated URL references. That is, a reference consisting of
only the authority and path portions of the identified resource, such
as
www.w3.org/Addressing/
or simply the DNS hostname on its own. Such references are primarily
intended for human interpretation rather than machine, with the
assumption that context-based heuristics are sufficient to complete
the URL (e.g., most hostnames beginning with "www" are likely to have
a URL prefix of "http://"). Although there is no standard set of
heuristics for disambiguating abbreviated URL references, many client
implementations allow them to be entered by the user and
heuristically resolved. It should be noted that such heuristics may
change over time, particularly when new URL schemes are introduced.
Since an abbreviated URL has the same syntax as a relative URL path,
abbreviated URL references cannot be used in contexts where relative
URLs are expected. This limits the use of abbreviated URLs to places
where there is no defined base URL, such as dialog boxes and off-line
advertisements.
What I understand from that is, Uri.IsWellFormedUriString
accepts strings that are in form of www.abc.com as valid URIs. But google.com is not accepted as an absolute URI whereas it's accepted as a relative URI because it conforms to relative path specification (paths can contain .).
Also, as a side note, if you want to use regular expression to parse a URI, you can read B. Parsing a URI Reference with a Regular Expression.