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?
c# regex
add a comment |
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?
c# regex
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
add a comment |
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?
c# regex
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
c# regex
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
add a comment |
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
add a comment |
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));
add a comment |
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
add a comment |
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
add a comment |
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));
add a comment |
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));
add a comment |
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));
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));
answered 21 hours ago
Fabio
18.6k22044
18.6k22044
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
edited 21 hours ago
answered 21 hours ago
Ashkan Mobayen Khiabani
18.7k1563113
18.7k1563113
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
edited 21 hours ago
answered 21 hours ago
Pushpesh Kumar Rajwanshi
2,0311716
2,0311716
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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