C# 获取C中特定单词后的字符串#
我有一个与此html相关的字符串:C# 获取C中特定单词后的字符串#,c#,.net,string,C#,.net,String,我有一个与此html相关的字符串: <div class="cnt_listas"><ol id="listagem1" class="cols_2"> <li><a href="/laura-pausini/73280/">16/5/74</a></li> <li><a href="/laura-pausini/73280/traducao.html">16/5/74</a></l
<div class="cnt_listas"><ol id="listagem1" class="cols_2">
<li><a href="/laura-pausini/73280/">16/5/74</a></li>
<li><a href="/laura-pausini/73280/traducao.html">16/5/74</a></li>
</div>
我需要获取
和
之间的所有文本。
此字符串中的文本可能与此不同,它是网站的结果。
我怎样才能得到这部分文字
在这种情况下,我需要的文本是:
<li><a href="/laura-pausini/73280/">16/5/74</a></li>
<li><a href="/laura-pausini/73280/traducao.html">16/5/74</a></li>
我不完全明白你在说什么。。。也许是这样:
string specificWord = stringWhtml.Substring(stringWhtml.IndexOf("cols_2") + 8, stringWhtml.IndexOf("</div>"));
string specificWord=stringWhtml.Substring(stringWhtml.IndexOf(“cols_2”)+8,stringWhtml.IndexOf(“”);
我几周前在Stackoverflow上找到的这段代码需要相同的算法,这段代码怎么样
private IEnumerable<string> GetSubStrings(string input, string start, string end)
{
Regex r = new Regex(Regex.Escape(start) + "(.*?)" + Regex.Escape(end));
MatchCollection matches = r.Matches(input);
foreach (Match match in matches)
yield return match.Groups[1].Value;
}
private IEnumerable GetSubstring(字符串输入、字符串开始、字符串结束)
{
Regex r=newregex(Regex.Escape(start)+“(.*?”+Regex.Escape(end));
MatchCollection matches=r.matches(输入);
foreach(匹配中的匹配)
产生返回匹配。组[1]。值;
}
编辑:是此代码的源代码
编辑2:要反驳我回答中的一条评论,请看一看。这不是解析HTML的最佳方法,但这里有一个扩展方法,它通常会以您询问的方式处理字符串:
public static string Between(this string source, string start, string end)
{
// Find the first occurence of the start string
var i = source.IndexOf(start);
if (i < 0)
return string.Empty;
// Advance past the start string
i += start.Length;
// Find the next occurence of the end string
var j = source.IndexOf(end, i);
if (j < 0)
return string.Empty;
// Return the string found between the positions
return source.Substring(i, j - i);
}
根据需要操纵边缘情况(未找到字符串等)我将用于解析html
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);
var h = doc.DocumentNode.SelectSingleNode("//ol[@id='listagem1']").InnerHtml;
在什么之间?此外,到目前为止,您尝试了什么?来吧,在HTML解析器上搜索-1您考虑过一个合适的HTML解析库吗?比如HtmlAgilityPack?eeewww Regex for HTML..@SimonWhitehead:我在回答中给你留了一个便条。我如何处理返回的值?IEnumerable我能把它转换成字符串吗?@leandrobilio:List results=resultnumerable.ToList()怎么样代码>或
字符串[]结果=resultEnumerable.Cast().ToArray()代码>?这将返回包含所有事件的集合。
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);
var h = doc.DocumentNode.SelectSingleNode("//ol[@id='listagem1']").InnerHtml;