且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

删除空格,除了前置标签中的空格

更新时间:2022-12-28 13:09:11

您不能分两步来做吗?换句话说,删除空格,然后替换< pre>".与< pre> \ n"和</pre>"一起使用
Can''t you do it in two steps? In other words, remove the whitespace and then replace "<pre>" with "<pre>\n" and "</pre>" with "\n</pre>".


,您可以找到在打开PRE(不紧随其后的PRE)之后不存在且不存在的所有空白在关闭PRE之前存在(不是在开始PRE之前).这将需要负的前瞻性和负性的后瞻性,而它们本身又包含负的前瞻性和负性的后瞻性.我从来没有尝试过将否定的前瞻性/后顾之忧结合在一起,所以我不确定它是否会起作用,但是似乎这是可行的方法.我会让您找出正则表达式,但伪正则表达式将是这样的:

You can find all whitespace that does not exist after an opening PRE (that isn''t followed by a closing PRE) and that does not exist before a closing PRE (that isn''t preceeded by an opening PRE). That would require a negative lookahead and a negative lookbehind that themselves contain a negative lookahead and a negative lookbehind. I''ve never tried combining negative lookaheads/lookbehinds like that, so I''m not sure it would work, but it seems like that would be the way to go. I''ll let you figure out the regex, but pseudo-regex would be something like this:

(must not contain a PRE that is not followed by a /PRE)
(?<=(.|\n)*)
whitespace
(?=(.|\n)*)
(must not contain a /PRE that is not preceeded by a PRE)



这是一个看起来很消极的样子:



Here is what a negative lookbehind looks like:

(?<!\<PRE\>)


这是一个负面的前瞻样子:


Here is what a negative lookahead looks like:

(?!\<\/PRE\>)


您必须做一些测试才能使用嵌套,因为我不确定它是如何工作的.

查看此参考,以获取更多C#正则表达式帮助.


You''ll have to do some testing to play with the nesting, as I am not quite sure how that works.

Check out this reference for more C# regex help.