使用regex c#从iframe获取url src

时间:2014-03-14 11:28:52

标签: c# regex soundcloud

我正在尝试从iframe获取soundcloud播放列表ID,但在c#中,iframe标签会创建转义样本:

"<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>"

如何使用此iframe代码使用正则表达式获取播放列表ID? 这是ID 26104012

4 个答案:

答案 0 :(得分:2)

您可以使用此正则表达式:

playlists/+([\d]+)

答案 1 :(得分:2)

如果ID总是有8位数,请尝试以下方法:

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;

或者,如果它始终位于网址的第一部分,请使用:

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("&", "");

答案 2 :(得分:1)

您可以使用以下代码匹配该号码:

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;
}

答案 3 :(得分:0)

我知道您想使用Regex来解析HTML,但根据我的经验,这绝不是一个好主意,HTML通常太多了,Regex可靠。如果我是你,我会使用像htmlagilitypack这样的HTML解析器。