Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 有人知道一个好的正则表达式来删除多余的空格吗?_Python_Regex - Fatal编程技术网

Python 有人知道一个好的正则表达式来删除多余的空格吗?

Python 有人知道一个好的正则表达式来删除多余的空格吗?,python,regex,Python,Regex,可能重复: 试图找出如何编写给定字符串的正则表达式: "hi this is a test" 我可以把它变成 "hi this is a test" 其中,空白被规范化为仅一个空格 有什么想法吗?非常感谢它需要是正则表达式吗 我只要用 new_string = " ".join(re.split(s'\s+', old_string.strip())) 塞德 您仍然使用正则表达式;)我想当我写出来的时候,实际上:) sed 's/[ ]\{2,\}/ /g'

可能重复:

试图找出如何编写给定字符串的正则表达式:

"hi     this       is a  test"
我可以把它变成

"hi this is a test"
其中,空白被规范化为仅一个空格


有什么想法吗?非常感谢

它需要是正则表达式吗

我只要用

new_string = " ".join(re.split(s'\s+', old_string.strip()))
塞德


您仍然使用正则表达式;)我想当我写出来的时候,实际上:)
 sed 's/[  ]\{2,\}/ /g'
import re    
re.sub("\s+"," ",string)