0

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

Rahil Wazir
  • 9,801
  • 11
  • 41
  • 63
comfreakph
  • 519
  • 2
  • 5
  • 20

4 Answers4

2

You could use this regex :

playlists/+([\d]+)
Luke Marlin
  • 1,388
  • 8
  • 24
2

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("&", "");
Oscar Bralo
  • 1,904
  • 12
  • 12
1

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;
}
mikeyq6
  • 1,128
  • 13
  • 27
0

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.

  • i'm not getting the html just the soundcloudid html in here are changeable but the regex above i checked is more reliable coz it gets the playlist source – comfreakph Mar 14 '14 at 14:22