Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.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# 4.0 如何得到正则表达式_C# 4.0_Expression - Fatal编程技术网

C# 4.0 如何得到正则表达式

C# 4.0 如何得到正则表达式,c#-4.0,expression,C# 4.0,Expression,我想删除文本中的google广告html,如 xxxxxxx<div class="gg200x300" style="padding: 19px; margin: 0px 22px 0px 0px; overflow: hidden; text-align: center; font-size: 0px; line-height: 0; float: left; border: 1px solid rgb(229, 229, 229); color: rgb(37, 37, 37); f

我想删除文本中的google广告html,如

xxxxxxx<div class="gg200x300" style="padding: 19px; margin: 0px 22px 0px 0px; overflow: hidden; text-align: center; font-size: 0px; line-height: 0; float: left; border: 1px solid rgb(229, 229, 229); color: rgb(37, 37, 37); font-family: 宋体, sans-serif;"><iframe src="http://g.163.com/r?site=netease&amp;affiliate=news&amp;cat=article&amp;type=logo300x250&amp;location=13" width="300" height="250" frameborder="no" border="0" marginwidth="0" marginheight="0" scrolling="no"></iframe></div>yyyyyy
如何使用c设置正则表达式,您能描述一下为什么使用正则表达式吗? 谢谢。

如果它总是在div中,您可以这样做

if (a.IndexOf("<div") > 0)
{
   Console.WriteLine(a.Remove(a.IndexOf("<div"),a.IndexOf("</div>")-1)); 
   //output xxxxxxxyyyyyy
}
然后基于此进行搜索

 if (a.IndexOf("<div id='googleadd'>") > 0)
 {
  :
  :
 }

如果它总是同一个类,那么使用@gmail user的方法就很容易了,但改为:

if (a.IndexOf("<div") > 0)
{
    if (a.Substring(a.IndexOf("<div")).Contains("class=\"gg200x300\""))
    {
        Console.WriteLine(a.Remove(a.IndexOf("<div"),a.IndexOf("</div>")-1)); 
        //output xxxxxxxyyyyyy
    }
}

我不会为此使用正则表达式,因为它对于您真正寻找的内容来说过于复杂,并且可能会产生误报,除非非常具体。查找某个类的div并将其删除非常简单。

添加的内容是什么?删除这需要更多的过滤。目前的代码将删除任何div元素。必须根据iframe进行替换,这样常规的express]+>]+>就可以正常工作。
 if (a.IndexOf("<div id='googleadd'>") > 0)
 {
  :
  :
 }
if (a.IndexOf("<div") > 0)
{
    if (a.Substring(a.IndexOf("<div")).Contains("class=\"gg200x300\""))
    {
        Console.WriteLine(a.Remove(a.IndexOf("<div"),a.IndexOf("</div>")-1)); 
        //output xxxxxxxyyyyyy
    }
}