如何删除多余的回报和空间通过正则表达式的字符串? [英] How to remove extra returns and spaces in a string by regex?

查看:133
本文介绍了如何删除多余的回报和空间通过正则表达式的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将一个HTML code为纯text.But还有很多额外的回报,spaces.How删除它们?

I convert a HTML code to plain text.But there are many extra returns and spaces.How to remove them?

推荐答案

我假设你想


  • 找到两个或多个连续的空格,并用一个空格替换它们,和

  • 找到两个或多个连续换行符,并用一个换行符替换它们。

如果这是正确的,那么你可以使用

If that's correct, then you could use

resultString = Regex.Replace(subjectString, @"( |\r?\n)\1+", "$1");

本保持完好空白的原来的类型,也是正确的preserves Windows行结尾。如果你也想凝结多个选项卡合而为一,使用

This keeps the original "type" of whitespace intact and also preserves Windows line endings correctly. If you also want to "condense" multiple tabs into one, use

resultString = Regex.Replace(subjectString, @"( |\t|\r?\n)\1+", "$1");

要凝聚换行符和空格的字符串(任意数量的每个)集成到一个新行,用

To condense a string of newlines and spaces (any number of each) into a single newline, use

resultString = Regex.Replace(subjectString, @"(?:(?:\r?\n)+ +){2,}", @"\n");

这篇关于如何删除多余的回报和空间通过正则表达式的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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