且构网

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

C# - 在标记之间移除HTML源代码中的空格?

更新时间:2023-02-02 13:54:38

Rasik发送这里它也是你的解决方案。

  Regex.Replace(html,@\s *(< [>] +>)\ s *,$ 1,RegexOptions.Singleline) ; 

常规会按照原样加上标记和周围的空格字符,并用标记对其进行更改。 / b>

编辑:
更好的解决方案适用于Micheal示例

  Regex.Replace(txtSource.Text,
@\s *(?< capture><(?< markUp> \ w +)>。*< ; \ / \k< markUp>>)\s *,$ {capture},RegexOptions.Singleline);

这个正则表达式会检测标记标记,不会改变它里面的内容并删除空格侧。
还有其他一些案例可以看。就像没有结束标签的标记一样。


I am currently working on a program that allows me to enter HTML source code into a RichTextBox control and removes the spaces from in between markups. The only problem is, I am not sure how I can differentiate between the spaces BETWEEN the markups and the spaces INSIDE the markups. Obviously, removing the spaces inside the markups would be bad. Any ideas as to how I can tell the difference?

Example: (before white space is removed)

<p>blahblahblah</p>                  <p>blahblahblah</p>

Example: (after white space is removed)

<p>blahblahblah</p><p>blahblahblah</p>

the solution in the link that Rasik sent here it's a solution for you too

Regex.Replace(html, @"\s*(<[^>]+>)\s*", "$1", RegexOptions.Singleline);

The regular take the markup as it is and the around space characters and change it with the markup.

Edit: A better solution that work for Micheal example

Regex.Replace(txtSource.Text,
            @"\s*(?<capture><(?<markUp>\w+)>.*<\/\k<markUp>>)\s*", "${capture}", RegexOptions.Singleline);

this regular expression will detect the markup tags and don't change what it's inside and remove the spaces out side. There's some other cases to look to it too. Like the markup without ending tags.