简体   繁体   English

从换行符除外的多行字符串中删除/替换所有空格

[英]Remove/Replace all whitespaces from a multi-lines string, except newline characters

I want to remove all whitespace characters from a multi-line string using regex. 我想使用正则表达式从多行字符串中删除所有空格字符。 What I am looking for is something like: 我正在寻找的是:

exp = re.compile("\s-[\r\n]")
exp.sub('', text)

Is there a regex that does the above. 是否有正则表达式执行上述操作。 Since the text is unicode, which has a possibility of other characters that could form the \\s class besides [\\t\\v\\f \\r\\n] , I cannot use [\\t\\v\\f ] . 由于text是unicode,除了[\\t\\v\\f \\r\\n]之外,其他字符可能形成\\s类,我不能使用[\\t\\v\\f ]

Try this double-negative character class: 试试这个双阴性字符类:

[^\S\r\n]

Example: http://rubular.com/r/t2Ahjs9UzF 示例: http//rubular.com/r/t2Ahjs9UzF

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM