删除空格,除了前置标签中的空格 [英] Remove whitespace except in pre tags

查看:113
本文介绍了删除空格,除了前置标签中的空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个替换的正则表达式可以从HTML文档中删除空格:/\s\s+/

是否可以删除除预标记内的所有空白?

例如.

I have a replacing regular expression to remove whitespace from a HTML document: /\s\s+/

Is there a way to remove all whitespace except from within pre tags?

eg.

<html>
<head>
	<title>Rawr</title>
</head>
<body>

<pre>
	Some stuff
</pre>

</body>
</html>

看起来像这样:

<html><head><title>Rawr</title></head><body><pre>
	Some stuff
</pre></body></html>

这甚至可能吗?

推荐答案

您不能分两步来做吗?换句话说,删除空格,然后替换< 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.


这篇关于删除空格,除了前置标签中的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆