Python re.sub不将撇号转换为空格
说明 我试图用Python re.sub不将撇号转换为空格,python,python-3.x,regex,Python,Python 3.x,Regex,说明 我试图用re.sub()替换空格中的所有撇号和逗号 代码 content = "he knew that the people were right. I'm I'm I'm I'm I'm" content = re.sub("^[^*$<,>?!']*$", ' ', content) OUTPUT: "he knew that the people were right. I'm I'm I'm I'm I'm"
re.sub()
替换空格中的所有撇号和逗号
代码
content = "he knew that the people were right. I'm I'm I'm I'm I'm"
content = re.sub("^[^*$<,>?!']*$", ' ', content)
OUTPUT:
"he knew that the people were right. I'm I'm I'm I'm I'm"
content=“他知道人们是对的。我,我,我,我,我”
content=re.sub(“^[^*$?!”]*$”,“”,content)
输出:
“他知道人们是对的。我我我我我我我”
这将返回相同的字符串,并且不会将任何内容转换为空格
我不确定我做错了什么。我不确定您是如何得出替换代码的,但我恐怕这太离谱了。如前所述,它说“只有当字符串不包含星号、美元符号、小于号、逗号、大于号、问号、感叹号或撇号时,才能用单个空格替换整个字符串。” 如果要用空格替换撇号和逗号,正则表达式要简单一些:
>>> content = "he knew that the people were right. I'm I'm I'm I'm I'm"
>>> re.sub(r"[,']", ' ', content)
'he knew that the people were right. I m I m I m I m I m'
正则表达式就是这样的:
[,'] matches either a comma or an apostrophe
一般来说,正则表达式构造[…]
,称为字符类,与括号中包含的任何字符匹配。有一些例外情况,其中一个是在原始代码中使用的:如果[
后面的第一个字符是插入符号(^
),则会否定字符类,使其与括号中剩余字符以外的任何单个字符相匹配
原来的正则表达式分解如下:
^ match only at the start of the string
[^...] match anything EXCEPT these characters
* 0 or more times
$ match only at the end of the string
结果是正则表达式与整个字符串匹配(因为^和$),并且仅当该字符串不包含插入符号后括号内的任何字符时,匹配才会成功。如果匹配成功,因为它匹配整个字符串,所以整个字符串将被替换-这意味着即使原始
re.sub
与字符串匹配,它也只会返回一个空格,无论输入字符串是什么s、 我建议使用.replace()
函数,如下所示:
content = "he knew that the people were right. I'm I'm I'm I'm I'm"
content = content.replace("'", " ")
print(content)
现在的输出是:
he knew that the people were right. I m I m I m I m I m
希望这有帮助
^
匹配字符串的开头*
匹配字符串的结尾content = "he knew that(,) the people were right. I'm I'm I'm I'm I'm"
re.sub("[\',\,]", ' ', content)