Python 使用正则表达式从字符串中删除空引号
我想使用正则表达式从字符串中删除标点符号,如“”、Python 使用正则表达式从字符串中删除空引号,python,regex,Python,Regex,我想使用正则表达式从字符串中删除标点符号,如“”、、、“”、“”。到目前为止,我编写的代码只删除了那些在它们之间留有空格的代码。如何删除空的,如“”, 我的预期结果: hey how is the what are you doing how is everything 您可以使用此正则表达式匹配所有此类引号: r'([\'"`])\s*\1\s*' 代码: >>> s = "hey how ' ' is the ` ` what are '' you doing `` h
、、“”、“”。到目前为止,我编写的代码只删除了那些在它们之间留有空格的代码。如何删除空的,如“”,
我的预期结果:
hey how is the what are you doing how is everything
您可以使用此正则表达式匹配所有此类引号:
r'([\'"`])\s*\1\s*'
代码:
>>> s = "hey how ' ' is the ` ` what are '' you doing `` how is everything"
>>> print (re.sub(r'([\'"`])\s*\1\s*', '', s))
hey how is the what are you doing how is everything
正则表达式详细信息:
>>> s = "hey how ' ' is the ` ` what are '' you doing `` how is everything"
>>> print (re.sub(r'([\'"`])\s*\1\s*', '', s))
hey how is the what are you doing how is everything
:匹配一个给定的引号并将其捕获到组1中([\'“`])
:匹配0个或多个空格\s*
:使用组#1的反向引用,确保我们匹配相同的收盘报价\1
:匹配0个或多个空格\s*
您可以使用此正则表达式匹配所有此类引号:
r'([\'"`])\s*\1\s*'
代码:
>>> s = "hey how ' ' is the ` ` what are '' you doing `` how is everything"
>>> print (re.sub(r'([\'"`])\s*\1\s*', '', s))
hey how is the what are you doing how is everything
正则表达式详细信息:
>>> s = "hey how ' ' is the ` ` what are '' you doing `` how is everything"
>>> print (re.sub(r'([\'"`])\s*\1\s*', '', s))
hey how is the what are you doing how is everything
:匹配一个给定的引号并将其捕获到组1中([\'“`])
:匹配0个或多个空格\s*
:使用组#1的反向引用,确保我们匹配相同的收盘报价\1
:匹配0个或多个空格\s*
在这种情况下,为什么不匹配所有单词字符,然后加入它们
' '.join(re.findall('\w+',s))
# 'hey how is the what are you doing how is everything'
在这种情况下,为什么不匹配所有单词字符,然后加入它们呢
' '.join(re.findall('\w+',s))
# 'hey how is the what are you doing how is everything'
您期望的输出是什么?刚才提到过。在空格后使用
?
以匹配它(可选)re.sub(“'?”|“?”,“,”,s)
为什么要使用正则表达式?您可以创建一个新字符串,在原始字符串中循环,并在新字符串中添加字符(如果它们不是标点符号):newstr=”“for char in mystr:if char not in(“”,…(在此处放置标点符号)):newstr+=char
要删除中间带/不带空格的成对的引号吗?不能在不匹配所有引号的情况下只匹配“
之类的内容。您的预期输出是什么?刚才提到过。可以选择在空格后使用?
。re.sub(“?”|“?”,”,s)
为什么要使用正则表达式?您可以创建一个新字符串,在原始字符串中循环,并在新字符串中添加字符(如果不是标点符号):newstr=”“for char in mystr:if char not in(“”,…(在此处放置标点符号)):newstr+=char
要删除中间有/没有空格的成对的引号吗?你不能只匹配像“
这样的东西而不匹配所有的引号。