Extract ISP From hostname C#









up vote
-2
down vote

favorite












I am using ipstack to get information about the user's IP. It gives me the hostname, but I would like to use the hostname to get the ISP. The hostname I am getting is:




d9687b05.cm-24.dynamic.ziggo.nl




Where I would like to extract the ziggo.nl <-- thats the ISP.



Of course it wont allways be ziggo, and it wont allways be .nl, so how would I go about getting that part of the hostname on different ones?










share|improve this question























  • you always want last two part?
    – Ashkan Mobayen Khiabani
    21 hours ago










  • What about aaisp.net.uk ? You will need to consider the publicsuffix.org to solve that problem.
    – Corion
    21 hours ago










  • See also stackoverflow.com/questions/52641486/…
    – Corion
    21 hours ago










  • Possible duplicate of Regex to strip domain from URL for all scenarios?
    – Corion
    21 hours ago














up vote
-2
down vote

favorite












I am using ipstack to get information about the user's IP. It gives me the hostname, but I would like to use the hostname to get the ISP. The hostname I am getting is:




d9687b05.cm-24.dynamic.ziggo.nl




Where I would like to extract the ziggo.nl <-- thats the ISP.



Of course it wont allways be ziggo, and it wont allways be .nl, so how would I go about getting that part of the hostname on different ones?










share|improve this question























  • you always want last two part?
    – Ashkan Mobayen Khiabani
    21 hours ago










  • What about aaisp.net.uk ? You will need to consider the publicsuffix.org to solve that problem.
    – Corion
    21 hours ago










  • See also stackoverflow.com/questions/52641486/…
    – Corion
    21 hours ago










  • Possible duplicate of Regex to strip domain from URL for all scenarios?
    – Corion
    21 hours ago












up vote
-2
down vote

favorite









up vote
-2
down vote

favorite











I am using ipstack to get information about the user's IP. It gives me the hostname, but I would like to use the hostname to get the ISP. The hostname I am getting is:




d9687b05.cm-24.dynamic.ziggo.nl




Where I would like to extract the ziggo.nl <-- thats the ISP.



Of course it wont allways be ziggo, and it wont allways be .nl, so how would I go about getting that part of the hostname on different ones?










share|improve this question















I am using ipstack to get information about the user's IP. It gives me the hostname, but I would like to use the hostname to get the ISP. The hostname I am getting is:




d9687b05.cm-24.dynamic.ziggo.nl




Where I would like to extract the ziggo.nl <-- thats the ISP.



Of course it wont allways be ziggo, and it wont allways be .nl, so how would I go about getting that part of the hostname on different ones?







c# regex






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 21 hours ago









Tân Nguyễn

2,99132048




2,99132048










asked 22 hours ago









Martijn Deleij

54




54











  • you always want last two part?
    – Ashkan Mobayen Khiabani
    21 hours ago










  • What about aaisp.net.uk ? You will need to consider the publicsuffix.org to solve that problem.
    – Corion
    21 hours ago










  • See also stackoverflow.com/questions/52641486/…
    – Corion
    21 hours ago










  • Possible duplicate of Regex to strip domain from URL for all scenarios?
    – Corion
    21 hours ago
















  • you always want last two part?
    – Ashkan Mobayen Khiabani
    21 hours ago










  • What about aaisp.net.uk ? You will need to consider the publicsuffix.org to solve that problem.
    – Corion
    21 hours ago










  • See also stackoverflow.com/questions/52641486/…
    – Corion
    21 hours ago










  • Possible duplicate of Regex to strip domain from URL for all scenarios?
    – Corion
    21 hours ago















you always want last two part?
– Ashkan Mobayen Khiabani
21 hours ago




you always want last two part?
– Ashkan Mobayen Khiabani
21 hours ago












What about aaisp.net.uk ? You will need to consider the publicsuffix.org to solve that problem.
– Corion
21 hours ago




What about aaisp.net.uk ? You will need to consider the publicsuffix.org to solve that problem.
– Corion
21 hours ago












See also stackoverflow.com/questions/52641486/…
– Corion
21 hours ago




See also stackoverflow.com/questions/52641486/…
– Corion
21 hours ago












Possible duplicate of Regex to strip domain from URL for all scenarios?
– Corion
21 hours ago




Possible duplicate of Regex to strip domain from URL for all scenarios?
– Corion
21 hours ago












3 Answers
3






active

oldest

votes

















up vote
0
down vote













Without Regex and extra explanations it can be done in readable and comprehensible way ;)



var names = hostname.split(".");
var notRequired = Math.Max(0, names.Count() - 2);

var isp = string.Join(".", names.skip(notRequired));





share|improve this answer



























    up vote
    0
    down vote













    Instead of using Regex I would do something like this:



    string isp = "";
    string split = hostname.Split('.');
    int len = split.Length;
    if(len >= 2)
    isp = $"split[len-2].split[len-1]";


    Or this:



     string isp = "";
    string hostname = "d9687b05.cm-24.dynamic.ziggo.nl";
    int a = hostname.LastIndexOf(".");
    if(a>0)a=hostname.LastIndexOf(".", a-1);
    if(a!=-1) isp = hostname.Substring(a+1);


    Live Demo






    share|improve this answer





























      up vote
      0
      down vote













      You can use this regex to capture the last two words,



      .*.(w+.w+)


      Demo



      Explanation:




      • .*. --> Matches zero or more any character followed by a literal dot


      • (w+.w+) --> Matches one or more word char followed by literal dot followed by one or more word char

      Here is a sample C# code,



       public static void Main(string args)

      var pattern = ".*\.(\w+\.\w+)";
      var match = Regex.Match("d9687b05.cm-24.dynamic.ziggo.nl", pattern);
      Console.WriteLine("DomainName: " + match.Groups[1].Value);



      This gives following output,



      DomainName: ziggo.nl





      share|improve this answer






















        Your Answer






        StackExchange.ifUsing("editor", function ()
        StackExchange.using("externalEditor", function ()
        StackExchange.using("snippets", function ()
        StackExchange.snippets.init();
        );
        );
        , "code-snippets");

        StackExchange.ready(function()
        var channelOptions =
        tags: "".split(" "),
        id: "1"
        ;
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function()
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled)
        StackExchange.using("snippets", function()
        createEditor();
        );

        else
        createEditor();

        );

        function createEditor()
        StackExchange.prepareEditor(
        heartbeatType: 'answer',
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader:
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        ,
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        );



        );













         

        draft saved


        draft discarded


















        StackExchange.ready(
        function ()
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53237189%2fextract-isp-from-hostname-c-sharp%23new-answer', 'question_page');

        );

        Post as a guest






























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        0
        down vote













        Without Regex and extra explanations it can be done in readable and comprehensible way ;)



        var names = hostname.split(".");
        var notRequired = Math.Max(0, names.Count() - 2);

        var isp = string.Join(".", names.skip(notRequired));





        share|improve this answer
























          up vote
          0
          down vote













          Without Regex and extra explanations it can be done in readable and comprehensible way ;)



          var names = hostname.split(".");
          var notRequired = Math.Max(0, names.Count() - 2);

          var isp = string.Join(".", names.skip(notRequired));





          share|improve this answer






















            up vote
            0
            down vote










            up vote
            0
            down vote









            Without Regex and extra explanations it can be done in readable and comprehensible way ;)



            var names = hostname.split(".");
            var notRequired = Math.Max(0, names.Count() - 2);

            var isp = string.Join(".", names.skip(notRequired));





            share|improve this answer












            Without Regex and extra explanations it can be done in readable and comprehensible way ;)



            var names = hostname.split(".");
            var notRequired = Math.Max(0, names.Count() - 2);

            var isp = string.Join(".", names.skip(notRequired));






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 21 hours ago









            Fabio

            18.6k22044




            18.6k22044






















                up vote
                0
                down vote













                Instead of using Regex I would do something like this:



                string isp = "";
                string split = hostname.Split('.');
                int len = split.Length;
                if(len >= 2)
                isp = $"split[len-2].split[len-1]";


                Or this:



                 string isp = "";
                string hostname = "d9687b05.cm-24.dynamic.ziggo.nl";
                int a = hostname.LastIndexOf(".");
                if(a>0)a=hostname.LastIndexOf(".", a-1);
                if(a!=-1) isp = hostname.Substring(a+1);


                Live Demo






                share|improve this answer


























                  up vote
                  0
                  down vote













                  Instead of using Regex I would do something like this:



                  string isp = "";
                  string split = hostname.Split('.');
                  int len = split.Length;
                  if(len >= 2)
                  isp = $"split[len-2].split[len-1]";


                  Or this:



                   string isp = "";
                  string hostname = "d9687b05.cm-24.dynamic.ziggo.nl";
                  int a = hostname.LastIndexOf(".");
                  if(a>0)a=hostname.LastIndexOf(".", a-1);
                  if(a!=-1) isp = hostname.Substring(a+1);


                  Live Demo






                  share|improve this answer
























                    up vote
                    0
                    down vote










                    up vote
                    0
                    down vote









                    Instead of using Regex I would do something like this:



                    string isp = "";
                    string split = hostname.Split('.');
                    int len = split.Length;
                    if(len >= 2)
                    isp = $"split[len-2].split[len-1]";


                    Or this:



                     string isp = "";
                    string hostname = "d9687b05.cm-24.dynamic.ziggo.nl";
                    int a = hostname.LastIndexOf(".");
                    if(a>0)a=hostname.LastIndexOf(".", a-1);
                    if(a!=-1) isp = hostname.Substring(a+1);


                    Live Demo






                    share|improve this answer














                    Instead of using Regex I would do something like this:



                    string isp = "";
                    string split = hostname.Split('.');
                    int len = split.Length;
                    if(len >= 2)
                    isp = $"split[len-2].split[len-1]";


                    Or this:



                     string isp = "";
                    string hostname = "d9687b05.cm-24.dynamic.ziggo.nl";
                    int a = hostname.LastIndexOf(".");
                    if(a>0)a=hostname.LastIndexOf(".", a-1);
                    if(a!=-1) isp = hostname.Substring(a+1);


                    Live Demo







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited 21 hours ago

























                    answered 21 hours ago









                    Ashkan Mobayen Khiabani

                    18.7k1563113




                    18.7k1563113




















                        up vote
                        0
                        down vote













                        You can use this regex to capture the last two words,



                        .*.(w+.w+)


                        Demo



                        Explanation:




                        • .*. --> Matches zero or more any character followed by a literal dot


                        • (w+.w+) --> Matches one or more word char followed by literal dot followed by one or more word char

                        Here is a sample C# code,



                         public static void Main(string args)

                        var pattern = ".*\.(\w+\.\w+)";
                        var match = Regex.Match("d9687b05.cm-24.dynamic.ziggo.nl", pattern);
                        Console.WriteLine("DomainName: " + match.Groups[1].Value);



                        This gives following output,



                        DomainName: ziggo.nl





                        share|improve this answer


























                          up vote
                          0
                          down vote













                          You can use this regex to capture the last two words,



                          .*.(w+.w+)


                          Demo



                          Explanation:




                          • .*. --> Matches zero or more any character followed by a literal dot


                          • (w+.w+) --> Matches one or more word char followed by literal dot followed by one or more word char

                          Here is a sample C# code,



                           public static void Main(string args)

                          var pattern = ".*\.(\w+\.\w+)";
                          var match = Regex.Match("d9687b05.cm-24.dynamic.ziggo.nl", pattern);
                          Console.WriteLine("DomainName: " + match.Groups[1].Value);



                          This gives following output,



                          DomainName: ziggo.nl





                          share|improve this answer
























                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            You can use this regex to capture the last two words,



                            .*.(w+.w+)


                            Demo



                            Explanation:




                            • .*. --> Matches zero or more any character followed by a literal dot


                            • (w+.w+) --> Matches one or more word char followed by literal dot followed by one or more word char

                            Here is a sample C# code,



                             public static void Main(string args)

                            var pattern = ".*\.(\w+\.\w+)";
                            var match = Regex.Match("d9687b05.cm-24.dynamic.ziggo.nl", pattern);
                            Console.WriteLine("DomainName: " + match.Groups[1].Value);



                            This gives following output,



                            DomainName: ziggo.nl





                            share|improve this answer














                            You can use this regex to capture the last two words,



                            .*.(w+.w+)


                            Demo



                            Explanation:




                            • .*. --> Matches zero or more any character followed by a literal dot


                            • (w+.w+) --> Matches one or more word char followed by literal dot followed by one or more word char

                            Here is a sample C# code,



                             public static void Main(string args)

                            var pattern = ".*\.(\w+\.\w+)";
                            var match = Regex.Match("d9687b05.cm-24.dynamic.ziggo.nl", pattern);
                            Console.WriteLine("DomainName: " + match.Groups[1].Value);



                            This gives following output,



                            DomainName: ziggo.nl






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited 21 hours ago

























                            answered 21 hours ago









                            Pushpesh Kumar Rajwanshi

                            2,0311716




                            2,0311716



























                                 

                                draft saved


                                draft discarded















































                                 


                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53237189%2fextract-isp-from-hostname-c-sharp%23new-answer', 'question_page');

                                );

                                Post as a guest














































































                                這個網誌中的熱門文章

                                How to read a connectionString WITH PROVIDER in .NET Core?

                                Node.js Script on GitHub Pages or Amazon S3

                                Museum of Modern and Contemporary Art of Trento and Rovereto