1

我在 VB.NET 中使用这些正则表达式,它们都单独工作:

texto = "ola <a href=""http://www.aiaia.pt"">coisas</a> tas <object> zborgind </object> bom <iframe src=""http://www.uiuiuiuiiu.pt""></iframe> hoje? <center>qq coisa</center> Já acabou, <script>document.write(""oi"")</script> ...agora!"
Dim regEx2 As New Regex("<a[^>]*>", RegexOptions.IgnoreCase Or RegexOptions.Multiline)
texto = regEx2.Replace(texto, String.Empty)
regEx2 = New Regex("</a[^>]*>", RegexOptions.IgnoreCase Or RegexOptions.Multiline)
texto = regEx2.Replace(texto, String.Empty)
regEx2 = New Regex("<object.*?>.*</object\s*?>", RegexOptions.IgnoreCase Or RegexOptions.Multiline)
texto = regEx2.Replace(texto, String.Empty)
regEx2 = New Regex("<iframe.*?>.*</iframe\s*?>", RegexOptions.IgnoreCase Or RegexOptions.Multiline)
texto = regEx2.Replace(texto, String.Empty)
regEx2 = New Regex("<center[^>]*>", RegexOptions.IgnoreCase Or RegexOptions.Multiline)
texto = regEx2.Replace(texto, String.Empty)
regEx2 = New Regex("</center[^>]*>", RegexOptions.IgnoreCase Or RegexOptions.Multiline)
texto = regEx2.Replace(texto, String.Empty)
regEx2 = New Regex("<script[^>]*>([\w\W]*?)</script>", RegexOptions.IgnoreCase Or RegexOptions.Multiline)
texto = regEx2.Replace(texto, String.Empty)

我怎样才能在一个表达式中加入它们?

我尝试将它们合二为一:

Dim texto2 As String = "ola <a href=""http://www.aiaia.pt"">coisas</a> tas <object> zborgind </object> bom <iframe src=""http://www.uiuiuiuiiu.pt""></iframe> hoje? <center>qq coisa</center> Já acabou, <script>document.write(""oi"")</script> ...agora!"

Dim re As New Regex( _
    "^<a[^>]*>$" & _
    "^</a[^>]*>$" & _
    "^<object.*?>.*</object\s*?>$" & _
    "^<iframe.*?>.*</iframe\s*?>$" & _
    "^<center[^>]*>$" & _
    "^</center[^>]*>$" & _
    "^<script[^>]*>([\w\W]*?)</script>$", _
    RegexOptions.IgnoreCase Or RegexOptions.Multiline _
)

但不起作用。你能帮助我吗?提前致谢。

4

1 回答 1

0

^$匹配一行的开始和结束。它们不分隔正则表达式中的子模式。改为使用|;它匹配左侧或右侧。

Dim re As New Regex( _
    "<a[^>]*>" & _
    "|</a[^>]*>" & _
    "|<object.*?>.*</object\s*?>" & _
    "|<iframe.*?>.*</iframe\s*?>" & _
    "|<center[^>]*>" & _
    "|</center[^>]*>" & _
    "|<script[^>]*>([\w\W]*?)</script>", _
    RegexOptions.IgnoreCase Or RegexOptions.Multiline _
)
于 2013-10-25T12:21:59.477 回答