Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 选择特定单词后的子字符串_C#_Html_Asp.net_Regex_String - Fatal编程技术网

C# 选择特定单词后的子字符串

C# 选择特定单词后的子字符串,c#,html,asp.net,regex,string,C#,Html,Asp.net,Regex,String,从这样的字符串 <iframe width="560" height="315" src="https://www.youtube.com/embed/KRFHiBW9RE8" frameborder="0" allowfullscreen></iframe> 我只需要选择源,所以单词src=“我需要的字符串” 我尝试过使用单词src=“的索引,但是链接没有固定数量的字符来设置结尾。如果您试图解析一些HTML代码,那么最好使用 但在这种情况下,它只是您从某处获得的一

从这样的字符串

<iframe width="560" height="315" src="https://www.youtube.com/embed/KRFHiBW9RE8" frameborder="0" allowfullscreen></iframe>

我只需要选择源,所以单词src=“我需要的字符串”


我尝试过使用单词src=“的索引,但是链接没有固定数量的字符来设置结尾。

如果您试图解析一些HTML代码,那么最好使用

但在这种情况下,它只是您从某处获得的一组字符串,您还可以使用:

string s=”“;
var match=Regex.match(s,“src=\”(.*?\”);
字符串src;
如果(匹配成功)
src=match.Groups[1]。值;

一个简单的实现,我假设您有一个字符串作为输入:

string input = "<iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/KRFHiBW9RE8\" frameborder=\"0\" allowfullscreen></iframe>";

if (input.Contains("src=\""))
{
    string output = input.Substring(input.IndexOf("src=\"") + 5);
    // output is: https://www.youtube.com/embed/KRFHiBW9RE8" frameborder="0" allowfullscreen></iframe>

    output = output.Substring(0, output.IndexOf("\""));
    // output is: https://www.youtube.com/embed/KRFHiBW9RE8
}
字符串输入=”;
if(input.Contains(“src=\”)
{
字符串输出=input.Substring(input.IndexOf(“src=\”)+5);
//输出为:https://www.youtube.com/embed/KRFHiBW9RE8“frameborder=“0”allowfullscreen>
output=output.Substring(0,output.IndexOf(“\”);
//输出为:https://www.youtube.com/embed/KRFHiBW9RE8
}

它肯定会错过像
src=“
”这样的边缘案例,但会给您一个开始的位置。显然,这也是一个可以用正则表达式解决的问题;我将把这个问题留给其他人回答。

我会尝试将所有属性拆分成一个数组,因为以后可能还需要其他一些属性。这样做还可以方便地访问“src”属性。所以我会这样做:

string iFrameString = "<iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/KRFHiBW9RE8\" frameborder=\"0\" allowfullscreen>";

//split properties based on spaces
string[] tagProps = iFrameString.Split(new Char[]{' '});

//get the property out.
string prop = "src=\"";
string source = Array.Find(tagProps, x => x.StartsWith(prop, StringComparison.InvariantCultureIgnoreCase));

string ModifiedSource = source.Substring(prop.Length,source.Length - prop.Length);
string iFrameString=”“;
//基于空间分割属性
string[]tagProps=iFrameString.Split(新字符[]{''});
//把房子搬出去。
string prop=“src=\”;
string source=Array.Find(tagProps,x=>x.StartsWith(prop,StringComparison.InvariantCultureIgnoreCase));
string ModifiedSource=source.Substring(prop.Length,source.Length-prop.Length);

这样做的好处是,您的数组中还有所有其他属性,如果需要,您可以将这些属性取出。

您还应该在找到src后搜索“后”=“您必须在服务器端执行此操作吗?如果不使用魔法字符串,考虑使用HTMGRelistyPayCo,你可以控制这个iFrAME吗?我是说你会加上这个还是从别的地方加上这个?
string iFrameString = "<iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/KRFHiBW9RE8\" frameborder=\"0\" allowfullscreen>";

//split properties based on spaces
string[] tagProps = iFrameString.Split(new Char[]{' '});

//get the property out.
string prop = "src=\"";
string source = Array.Find(tagProps, x => x.StartsWith(prop, StringComparison.InvariantCultureIgnoreCase));

string ModifiedSource = source.Substring(prop.Length,source.Length - prop.Length);