dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Thread: How to check if a url is valid from a windows application

  1. #1
    Join Date
    Aug 2004
    Location
    USA
    Posts
    90

    How to check if a url is valid from a windows application

    Hi
    I could read a string but I am looking if this is a valid URL and also how can I add this string to a listbox if it is a valid URL?
    Thanks a ton
    Response is appreciated
    Shash

  2. #2
    Join Date
    Nov 2002
    Location
    Singapore
    Posts
    1,890

    Lightbulb

    use Regex.
    and check for possible combinatorix.
    meaning
    http:
    or http://
    or
    http://www etc..

    normally // should be delimiter.

    Paresh
    - Software Architect

  3. #3
    Join Date
    Nov 2002
    Location
    Singapore
    Posts
    1,890

    Lightbulb

    check for expression

    ^(https?://)?(([0-9a-z_!~*'().&=+$%-]+?[0-9a-z_!~*'().&=+$%-]+@)?(([0-9]{1
    ,3}\.){3}[0-9]{1,3}|([0-9a-z_!~*'()-]+\.)*([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]
    \.[a-z]{2,6})(:[0-9]{1,4})?((/?)|(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$


    all other chars..
    for example

    use following anchors
    and codelets

    $regexp = "^(https?://)"; // http://
    $regexp .= "?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?"; //
    usernameassword@
    $regexp .= "(([0-9]{1,3}\.){3}[0-9]{1,3}"; // IP- 199.194.52.184
    $regexp .= "|"; // allows either IP or domain
    $regexp .= "([0-9a-z_!~*'()-]+\.)*"; // tertiary domain(s)- www.
    $regexp .= "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\."; // second level domain
    $regexp .= "[a-z]{2,6})"; // first level domain- .com or .museum
    $regexp .= "(:[0-9]{1,4})?"; // port number- :80
    $regexp .= "((/?)|"; // a slash isn't required if there is no file name
    $regexp .= "(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$";


    Paresh
    - Software Architect

  4. #4
    Join Date
    Aug 2004
    Location
    USA
    Posts
    90
    Hi Paresh
    Thank you for your reply.
    COuld you please elaborate your point. COuld you provide me with a sample code because I am new to C# and probably would understand if I get an example.
    Thank you
    Shash

  5. #5
    Join Date
    Jul 2010
    Posts
    3

    Re: How to check if a url is valid from a windows application

    Your url regex is good but I found a flaw.

    This is a valid URL (common for Google Alerts):
    http://www.google.com/url?q=http://www.trulymail.com/

    However, this will not match your regex. The problem is the double slash (//) after the q=.

    The only change you need to make to support this format is the last line. Where you wrote:
    $regexp .= "(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$";

    It should be
    $regexp .= "(/[0-9a-z_!~*'().;?:@&=+$,%#-/]+)+/?)$";

    I know this is a very old thread but hopefully this minor change will save someone some headache.

  6. #6
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,267

    Re: How to check if a url is valid from a windows application

    master21, thanks for helping, but remember this thread is over 6 years old.

    I am closing it now.
    [SIGPIC][/SIGPIC]
    All my Articles
    Hannes

  7. #7
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,267

    Re: How to check if a url is valid from a windows application

    I re - opened this thread, on a request.

    Hannes
    [SIGPIC][/SIGPIC]
    All my Articles
    Hannes

  8. #8
    Join Date
    Jul 2010
    Posts
    3

    Re: How to check if a url is valid from a windows application

    I thought I had the perfect regex for a URL but in the end, even "something: test@test.com" matched falsely as a URL.

    In the end, I used "^https?://." - that's right, http:// or https:// followed by anything.

    If you find a better regex for a url, please let us know.

  9. #9
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,060

    Re: How to check if a url is valid from a windows application

    regex always makes my head hurt.

  10. #10
    Join Date
    Feb 2004
    Location
    Seattle, USA
    Posts
    137

    Re: How to check if a url is valid from a windows application

    Why not use the System.Uri class?

  11. #11
    Join Date
    Jul 2010
    Posts
    3

    Re: How to check if a url is valid from a windows application

    Yes, regex can be less than easy.

    As for system.uri, it does have a TryCreate() method but it will return true even for a string like:
    "something: test" which is clearly not a valid URL. I'm not even sure it is a valid URL but perhaps it forgives not htmlencoding the space.

    The IsWellFormedUriString() looks a little more promising. It will not forgive the space in the above example, but it still returns true for:
    "something:test"

    So, it seems it is not sensitive to which are valid protocols and what the parameters of the protocol should be (e.g., mailto: should have an @ but not test@ since there is no TLD or other domain for that matter).

    Alas, the search goes on.

  12. #12
    Join Date
    Feb 2004
    Location
    Seattle, USA
    Posts
    137

    Re: How to check if a url is valid from a windows application

    The main problem with using a RegEx is that Uri formats change over time. For example, everyone 10 years ago thought that IPv4 was plenty & now we have IPv6 (a very different syntax). Eventually, your RegEx will become dated & you will have to provide support to add new formats as they arise.

    "blah:foo" is the same as "blah://foo", which is a valid uri (for example, http:bing.com) If you'd only like to support http or https, then you will need to validate the uri scheme.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)