Domanda

I'm trying to get the soundcloud playlist ID from iframe but in c# the iframe tags creates escape sample:

"<iframe width=\"100%\" height=\"450\" scrolling=\"no\" frameborder=\"no\" src=\"https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/playlists/26104012&amp;auto_play=false&amp;hide_related=false&amp;visual=true%22\"></iframe>"

How can I get the playlist id using regex with this iframe tags? This is the id 26104012

È stato utile?

Soluzione 3

You could match that number using the following code:

string search = "<iframe width=\"100%\" height=\"450\" scrolling=\"no\" frameborder=\"no\" src=\"https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/playlists/26104012&amp;auto_play=false&amp;hide_related=false&amp;visual=true%22\"></iframe>";
string sPattern = "^.*src=.*playlists\\/([0-9]+)&.*$";


Match match = Regex.Match(search, sPattern, RegexOptions.IgnoreCase);
// Here we check the Match instance.
if (match.Success)
{
    // Finally, we get the Group value and display it.
    string id = match.Groups[1].Value;
}

Altri suggerimenti

If the id always have 8 digits, try something like:

string text = "<iframe width=\"100%\" height=\"450\" scrolling=\"no\" frameborder=\"no\" src=\"https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/playlists/26104012&amp;auto_play=false&amp;hide_related=false&amp;visual=true%22\"></iframe>";
Regex r = new Regex(@"\d{8}");
string result = r.Match(text).Value;

Or if it´s always in the first part of the url, use this:

string text = "<iframe width=\"100%\" height=\"450\" scrolling=\"no\" frameborder=\"no\" src=\"https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/playlists/26104012&amp;auto_play=false&amp;hide_related=false&amp;visual=true%22\"></iframe>";
Regex r = new Regex(@"\d+&");
string t = r.Match(text).Value.Replace("&", "");

You could use this regex :

playlists/+([\d]+)

I know you wanted to use Regex to parse the HTML, but in my experience this is never a good idea, the HTML is usually too changeable for Regex to be reliable. If I were you I would use a HTML parser like htmlagilitypack.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top