c# - 如何编写多行 RegEx 表达式

标签 c# .net html vb.net regex

我有一个 vb.net 类,它在通过电子邮件发送结果之前清理一些 html。

这是我需要删除的一些 html 的示例:

    <div class="RemoveThis">
      Blah blah blah<br /> 
      Blah blah blah<br /> 
      Blah blah blah<br /> 
      <br /> 
    </div>

我现在已经在使用 RegEx 来完成我的大部分工作。 将上面的 block 替换为空的 RegEx 表达式会是什么样子?

我尝试了以下方法,但有些地方不对:

'html has all of my text
html = Regex.Replace(html, "<div.*?class=""RemoveThis"">.*?</div>", "", RegexOptions.IgnoreCase)

谢谢。

最佳答案

添加单行选项:

html = Regex.Replace(html, "<div.*?class=""RemoveThis"">.*?</div>", "", RegexOptions.IgnoreCase Or RegexOptions.Singleline)

来自 MSDN :

Singleline: Specifies single-line mode. Changes the meaning of the dot (.) so it matches every character (instead of every character except \n).

PS:用正则表达式解析HTML是discouraged .你的代码会因为这样的事情而失败:

<div class="RemoveMe">
    <div>bla</div>
    <div>bla</div>
</div>

关于c# - 如何编写多行 RegEx 表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2049553/

相关文章:

javascript - 加载集线器时出错。 HTML5/js 前端和 C# 后端

c# - (MSTest) 扩展 ExpectedExceptionBaseAttribute 隐藏测试失败解释

c# - 将具体类型的 ICollection 转换为具体类型接口(interface)的 ICollection

css - 媒体查询视觉错误,意外行为移动响应

html - 与CSS网格重叠的段落

c# - 位图缓存?何时以及如何使用?

c# - 循环遍历 .resx 文件中的所有资源

c# - 如何制作 System.Configuration.Install.Installer 以从安装项目中获取变量?

c# - 适用于任何文化的 DateTime.TryParse

javascript - 使用 Canvas 将图像添加到轮子上