C# 如果不使用html解析器,有没有一种方法,使用普通的string方法来获取两个指定字符串之间的字符串部分,在我的例子中是标记

C# 如果不使用html解析器,有没有一种方法,使用普通的string方法来获取两个指定字符串之间的字符串部分,在我的例子中是标记,c#,asp.net,linq,C#,Asp.net,Linq,假设我有以下字符串 "<description>This is the description,<strong> I want to retrieve this text</strong></description> and this is not the description." “这是说明,我想检索此文本,但这不是说明。” 我只想提取两个描述标记/字符串之间的字符串部分。我知道我可以安装和使用类似于html agility pack的东西

假设我有以下字符串

"<description>This is the description,<strong> I want to retrieve this text</strong></description> and this is not the description."
“这是说明,我想检索此文本,但这不是说明。”
我只想提取两个描述标记/字符串之间的字符串部分。我知道我可以安装和使用类似于html agility pack的东西,但我不希望这样做是为了一个目的。net XML解析器也不行,因为它不能很好地处理html。

var description=Regex.Match(s,@“(.*)).Groups[1];
var description = Regex.Match(s, @"<description>(.*)</description>").Groups[1];
var description=Regex.Match(s,@“(.*)).Groups[1];

您可以使用带有lookaround的正则表达式来匹配开始和结束标记:

string description = 
    Regex.Match(html, @"(?<=<description>).*?(?=</description>)").Value;
字符串描述=

Regex.Match(html,@)(?您可以使用带有lookaround的Regex来匹配开始和结束标记:

string description = 
    Regex.Match(html, @"(?<=<description>).*?(?=</description>)").Value;
字符串描述=

Match(html,@)(?您可以使用正则表达式通过以下代码获取描述标记之间的字符串

 Regex objPatterntable = new Regex("<description [^>]*?>.*?</description>", RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.IgnorePatternWhitespace);
Regex objPatterntable=new Regex(“]*?>*”),RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.IgnorePatternWhitespace);

您可以使用正则表达式通过以下代码获取描述标记之间的字符串

 Regex objPatterntable = new Regex("<description [^>]*?>.*?</description>", RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.IgnorePatternWhitespace);
Regex objPatterntable=new Regex(“]*?>*”),RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.IgnorePatternWhitespace);

对于您发布的确切问题或有限子集,可能没问题,否则,对于您发布的确切问题或有限子集,可能没问题,