locked
find url from a text file in C# RRS feed

  • Question

  • hi how to find website(URL) in .txt file Ex:this is a message that is saved on notepad this is my url http://www.google.com and visit this website,here how can i find that url(www.google.com)  in windows application

    Karthikn.s

    Friday, April 25, 2014 5:27 AM

Answers

  • Hi, you can use File.ReadAllText to read string from .txt file first and then use below regex to find url:

    (http|ftp|https):\/\/([\w\-_]+(?:(?:\.[\w\-_]+)+))([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?

    for example:

    string txt = "this is my url http://www.google.com and visit this website and this is my url http://www.yahoo.com";
    foreach (Match item in Regex.Matches(txt, @"(http|ftp|https):\/\/([\w\-_]+(?:(?:\.[\w\-_]+)+))([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?"))
    {
        Console.WriteLine(item.Value);
    }
    you will get http://www.google.com and http://www.yahoo.com

    Friday, April 25, 2014 6:11 AM

All replies

  • Do you have just the URL in the file or is there more text.  If there is more test I need a sample of the text file so I can just extract the URL from the rest of the characters in the file..

    jdweng

    Friday, April 25, 2014 6:08 AM
  • Hi, you can use File.ReadAllText to read string from .txt file first and then use below regex to find url:

    (http|ftp|https):\/\/([\w\-_]+(?:(?:\.[\w\-_]+)+))([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?

    for example:

    string txt = "this is my url http://www.google.com and visit this website and this is my url http://www.yahoo.com";
    foreach (Match item in Regex.Matches(txt, @"(http|ftp|https):\/\/([\w\-_]+(?:(?:\.[\w\-_]+)+))([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?"))
    {
        Console.WriteLine(item.Value);
    }
    you will get http://www.google.com and http://www.yahoo.com

    Friday, April 25, 2014 6:11 AM
  • sample  text file..........................

        

    <meta property="og:site_name" content="Yahoo" />
        <meta property="fb:app_id" content="90376669494" />

        <link rel="shortcut icon" href="https://s.yimg.com/rz/l/favicon.ico" />
        <meta name="application-name" content="Yahoo!">
        <meta name="msapplication-starturl" content="/">
        <meta name="msapplication-task" content="name=Yahoo Mail;action-uri=http://mail.yahoo.com/;icon-uri=http://www.yahoo.com/favicon.ico">
        <meta name="msapplication-task" content="name=Yahoo! Sports;action-uri=http://sports.yahoo.com;icon-uri=http://www.yahoo.com/favicon.ico">
        <meta name="msapplication-task" content="name=Yahoo! Finance;action-uri=http://finance.yahoo.com;icon-uri=http://www.yahoo.com/favicon.ico">

    -------------

    here  how to find all url


    Karthikn.s

    Friday, April 25, 2014 6:11 AM
  • thank you  this is working but one error now i give like "this is the text href="https://play.google.com/?hl=en&tab=w8">"

    Error:error CS1002: ; expected

    because double " so plz fix and help me


    Karthikn.s

    Friday, April 25, 2014 6:26 AM
  • thank you  this is working but one error now i give like "this is the text href="https://play.google.com/?hl=en&tab=w8">"

    Error:error CS1002: ; expected

    because double " so plz fix and help me


    Karthikn.s

    Hi, it should be:

    string txt = "this is the text href=\"https://play.google.com/?hl=en&tab=w8\">";

    Friday, April 25, 2014 6:33 AM
  • A link which will not answer but help you to choose one.

    http://mathiasbynens.be/demo/url-regex


    Mark Answered, if it solves your question and Vote if you found it helpful.
    Rohit Arora

    Friday, April 25, 2014 9:08 AM