0

I have a URL and need to extract the port, username and password from it and put them into an array. It looks like following.

http://myproject.ddns.net:8080/get.php?username=9zu7T54rt6&password=1Tbliu49iH&type=m3u_plus&output=ts

Can I use some other method without replaces or substring?

Jason H
  • 4,996
  • 6
  • 27
  • 49
  • 1
    Have you tried anything yet ? – Rahul May 25 '17 at 20:25
  • Possible duplicate of [What is a good regular expression to match a URL?](https://stackoverflow.com/questions/3809401/what-is-a-good-regular-expression-to-match-a-url) – Adrian May 25 '17 at 20:25
  • 1
    If you've tried something already, please post your code in your question. If you have not tried anything yet, please give something a try and then come back to us with specific issues you are stuck on. – Tim Hutchison May 25 '17 at 20:27
  • @Adrian: It's not a duplicate. OP is asking to extract data from URL not match if it's valid. – Rahul May 25 '17 at 20:27
  • Which only requires making the non-capturing groups capturing groups instead. – Adrian May 25 '17 at 20:28
  • IMO, you should use some scripting language to accomplish this, as simply regex will only go so far. [Here](http://rubular.com/r/AiZq37XXkw) is a very rough attempt. – Matt Clark May 25 '17 at 20:29
  • 1
    This is already built into the `Uri` class; investigate `new Uri(yourUri).Port`, and `Uri.GetComponents()`. – Dour High Arch May 25 '17 at 20:29

4 Answers4

1

One of the ways in C#

Get the query parameter

var parsedQuery = HttpUtility.ParseQueryString("http://myproject.ddns.net:8080/get.php?username=9zu7T54rt6&password=1Tbliu49iH&type=m3u_plus&output=ts");

Then, below will give the username

parsedQuery["username"] 

For Password:

parsedQuery["password"] 

For port you can use URI :

Uri uri = new Uri("http://myproject.ddns.net:8080/get.php?username=9zu7T54rt6&password=1Tbliu49iH&type=m3u_plus&output=ts");

Get the port by

uri.Port

Create an array or use whatever you require to club.

Tushar Gupta
  • 15,504
  • 1
  • 29
  • 47
  • 1
    that looks good but i think you need internet connection for it (i know i didn't telled this in my post) but we need to be able to add links to a list even when we are offline. The links we input are "100%" valid but i really like your method and will keep it in mind for my next project :) –  May 25 '17 at 20:36
  • But why u need an internet connection for this? can you explain ? – Tushar Gupta May 25 '17 at 20:37
  • i am sorry i thought that will also check if it's available –  May 25 '17 at 20:38
0

I don't know C#, but here's one that works for Python. It's pretty straightforward so you should be able to convert.

:(?P<port>[0-9]+).*username=(?P<username>[a-zA-Z0-9]+).*password=(?P<password>[a-zA-Z0-9]+)

The (?P<foo>bar) syntax is a named capture group that will put a variable matching the pattern 'bar' into a variable called 'foo' when you extract them.

OnNIX
  • 422
  • 4
  • 10
0

Here is another possible solution with pure C# regex:

var url = "http://myproject.ddns.net:8080/get.php?username=9zu7T54rt6&password=1Tbliu49iH&type=m3u_plus&output=ts";
var urlRegex = new Regex(@"(?<=(http(s)?://)?\w+(\.\w+)*:)\d+(?=/.*)?");
var usernameRegex = new Regex(@"(?<=(\?|&)username=).*?(?=&|$)", RegexOptions.IgnoreCase);
var passwordRegex = new Regex(@"(?<=(\?|&)password=).*?(?=&|$)", RegexOptions.IgnoreCase);

Console.WriteLine(urlRegex.Match(url));
Console.WriteLine(usernameRegex.Match(url));
Console.WriteLine(passwordRegex.Match(url));
LIvanov
  • 1,126
  • 12
  • 30
0

If there are any parts that don't change, e.g. if it's always the same url you could just replace it like this

string str = "http://myproject.ddns.net:8080/get.php?username=9zu7T54rt6&password=1Tbliu49iH&type=m3u_plus&output=ts"
str.Replace("http://myproject.ddns.net","");

This would leave you ":8080/get.php?username=9zu7T54rt6&password=1Tbliu49iH&type=m3u_plus&output=ts"

There is nothing stopping you repeating the process with another section.

As for regex you could use Regex.Match https://msdn.microsoft.com/en-us/library/twcw2f1c(v=vs.110).aspx to get the parts you want.

You could use ":\d{4}/" to get the port - you'd have to strip the leading ":" and trailing "/" though; this "username=\w*\&" to get the username - you'd have to strip the leading "username=" and trailing "&" though; and for the password you could use "password=\w*\&" - you'd have to strip the leading "password=" and trailing "&" though.

If you'd like to experiment with regex this site https://regex101.com/ is pretty good.

Gareth
  • 913
  • 6
  • 14