Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/331.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 re.sub()用于空格和制表符_Python_Regex - Fatal编程技术网

Python re.sub()用于空格和制表符

Python re.sub()用于空格和制表符,python,regex,Python,Regex,我正在尝试查找2行re.sub()行。第一种方法将任意数量的空格和/或制表符按任意顺序更改为空格。(也许这应该是两个独立的?) 第二种方法将任何不是字母或空格的内容更改为空字符串。我把目前为止我所知道的每一个都包括在下面 re.sub("?"," ",word) re.sub("?","",word) 问号所在的位置应该放什么?谢谢。 \s+查找所有空白,但要注意细节 [\t]+如果您只需要制表符和空格 代码:

我正在尝试查找2行
re.sub()
行。第一种方法将任意数量的空格和/或制表符按任意顺序更改为空格。(也许这应该是两个独立的?) 第二种方法将任何不是字母或空格的内容更改为空字符串。我把目前为止我所知道的每一个都包括在下面

re.sub("?"," ",word)
re.sub("?","",word)
问号所在的位置应该放什么?谢谢。

  • \s+
    查找所有空白,但要注意细节
  • [\t]+
    如果您只需要制表符和空格
代码:


第一个
将是
\s+
。第二个
[^a-zA-Z]
import re

word = "hello    this is \ta\ttest"
result = re.sub(r'\s+', " ", word) 

print(result)  # prints: hello this is a test
re.sub('\s+', ' ', word)