Regex
标签的内容

本文关键字:标签 pre Regex | 更新日期: 2023-09-27 18:16:28

我使用这个过滤器来缩小我的HTML。不幸的是,该过滤器还减少了<pre>标记内的代码,但我不希望更改它们。我怎么能改变正则,使他们不减少<pre>标签内的任何代码?

s = Regex.Replace(s, @"'s+", " ");
s = Regex.Replace(s, @"'s*'n's*", "'n");
s = Regex.Replace(s, @"'s*'>'s*'<'s*", "><");
s = Regex.Replace(s, @"<!--(.*?)-->", "");   //Remove comments

Regex <pre class=标签的内容" />

在该过滤器的开发人员提供此选项之前,您可以尝试以下操作:您可以向正则表达式添加一个嵌套的前瞻性断言,如果</pre>标记紧随其后(除非<pre>标记先出现),则该断言将阻止它们匹配。对于前三个正则表达式,这意味着:

s = Regex.Replace(s, @"(?s)'s+(?!(?:(?!</?pre'b).)*</pre>)", " ");
s = Regex.Replace(s, @"(?s)'s*'n's*(?!(?:(?!</?pre'b).)*</pre>)", "'n");
s = Regex.Replace(s, @"(?s)'s*'>'s*'<'s*(?!(?:(?!</?pre'b).)*</pre>)", "><");

解释的前瞻性断言:

(?!          # Assert that the following regex can't be matched here:
 (?:         # Match...
  (?!        #  (unless the following can be matched:
   </?pre'b  #  an opening or closing <pre> tag)
  )          #  (End of inner lookahead assertion)
  .          # ...any character (the (?s) makes sure that this includes newlines)
 )*          # Repeat any number of times
 </pre>      # Match a closing <pre> tag
)            # (End of outer lookahead assertion)

对于第四个正则表达式,我们必须首先确保.*?不匹配任何<pre>标签

s = Regex.Replace(s, @"(?s)<!--((?:(?!</?pre'b).)*?)-->(?!(?:(?!</?pre'b).)*</pre>)", "");

除此之外,regex的工作原理与上面一样。