Python删除大括号之间的空格
我正在使用re,但我没有任何运气。我想知道如何删除大括号之间的空格。比如说 我有这根绳子Python删除大括号之间的空格,python,regex,Python,Regex,我正在使用re,但我没有任何运气。我想知道如何删除大括号之间的空格。比如说 我有这根绳子 i want to go to the[ super mall ](place) 你看到“[超级商场]”里的空间了吗 什么能把这根绳子变成什么 i want to go to the [super mall](place) 如果能得到任何帮助,我将不胜感激。谢谢 除了删除括号周围的空格外,您还希望将[转换为[ 要处理此问题并删除括号中文本周围任何数量/类型的空格,可以执行以下操作
i want to go to the[ super mall ](place)
你看到“[超级商场]”里的空间了吗
什么能把这根绳子变成什么
i want to go to the [super mall](place)
如果能得到任何帮助,我将不胜感激。谢谢 除了删除括号周围的空格外,您还希望将[转换为[ 要处理此问题并删除括号中文本周围任何数量/类型的空格,可以执行以下操作
>>> import re
>>> text = 'i want to go to the[ super mall ](place)'
>>> re.sub(r'(\w)\[\s+(.*?)\s+\]', r'\1 [\2]', 'i want to go to the[ super mall ](place)')
'i want to go to the [super mall](place)'
我们可以更详细地查看re.sub
的前两个参数(最后一个参数只是要操作的字符串)
首先我们有这个正则表达式r'(\w)\[\s+(.*?)\s+\]'
匹配一个单词和一个括号,并使用括号存储该单词供以后使用,(匹配原始字符串中的[)(\w)\[
匹配一个或多个空白字符(匹配原始字符串中的\s+
)
匹配括号和空格中的任何文本,并存储此文本以供以后使用括号(匹配原始字符串中的(.*)
)super-mall
匹配一个或多个空白字符和右括号(匹配原始字符串中的\s+\]
)]
the
和super-mall
),并匹配了整个子字符串the[super-mall]
接下来,我们将使用以下模板替换整个子字符串:
\1[\2]
。这将使用先前存储的字符串分别替换\1
和\2
。除了删除括号周围的空格外,您还希望将的[
转换为的[
要处理此问题并删除括号中文本周围任何数量/类型的空格,可以执行以下操作
>>> import re
>>> text = 'i want to go to the[ super mall ](place)'
>>> re.sub(r'(\w)\[\s+(.*?)\s+\]', r'\1 [\2]', 'i want to go to the[ super mall ](place)')
'i want to go to the [super mall](place)'
我们可以更详细地查看re.sub
的前两个参数(最后一个参数只是要操作的字符串)
首先我们有这个正则表达式r'(\w)\[\s+(.*?)\s+\]'
匹配一个单词和一个括号,并使用括号存储该单词供以后使用,(匹配原始字符串中的[)(\w)\[
匹配一个或多个空白字符(匹配原始字符串中的\s+
)
匹配括号和空格中的任何文本,并存储此文本以供以后使用括号(匹配原始字符串中的(.*)
)super-mall
匹配一个或多个空白字符和右括号(匹配原始字符串中的\s+\]
)]
the
和super-mall
),并匹配了整个子字符串the[super-mall]
接下来,我们将使用模板替换整个子字符串:
\1[\2]
。这将使用先前存储的字符串分别替换\1
和\2
。我假设大括号是平衡的,不能嵌套
>>> import re
>>> s = 'i want to go to the[super mall](place) [ for real ]'
>>> re.sub('\[\s*(.*?)\s*\]', r'[\1]', s)
'i want to go to the[super mall](place) [for real]'
它不适用于多个。就像我想去[超级商场](地点)和[奶酪商场](地点) 我想是的
>>> s = 'i want to go to the [ super mall ](place) and [ cheese mall ](place)'
>>> re.sub('\[\s*(.*?)\s*\]', r'[\1]', s)
'i want to go to the [super mall](place) and [cheese mall](place)'
我假设大括号是平衡的,不能嵌套
>>> import re
>>> s = 'i want to go to the[super mall](place) [ for real ]'
>>> re.sub('\[\s*(.*?)\s*\]', r'[\1]', s)
'i want to go to the[super mall](place) [for real]'
它不适用于多个。就像我想去[超级商场](地点)和[奶酪商场](地点) 我想是的
>>> s = 'i want to go to the [ super mall ](place) and [ cheese mall ](place)'
>>> re.sub('\[\s*(.*?)\s*\]', r'[\1]', s)
'i want to go to the [super mall](place) and [cheese mall](place)'
您可以使用组和反向引用来解决此问题:
string = 'i want to go to the[ super mall ](place)'
re.sub('\[ (.+) \]', '[\g<1>]', string)
string='我想去[超级商场](地点)'
re.sub(“\[(.+)\]”,“[\g]”,字符串)
在这里,我假设你总是至少有一个字符,在括号内,每边加一个空格
这会给你
“我想去[超级商场](地点)
请参阅re docs:您可以使用组和反向引用来解决此问题:
string = 'i want to go to the[ super mall ](place)'
re.sub('\[ (.+) \]', '[\g<1>]', string)
string='我想去[超级商场](地点)'
re.sub(“\[(.+)\]”,“[\g]”,字符串)
在这里,我假设你总是至少有一个字符,在括号内,每边加一个空格
这会给你
“我想去[超级商场](地点)
请参阅re docs:大括号可以嵌套吗?这是否回答了您的问题?大括号可以嵌套吗?大括号可以嵌套吗?大括号可以嵌套吗?大括号可以嵌套吗?大括号可以嵌套吗?大括号可以嵌套吗[。您的代码不会这样做。您应该避免覆盖内置对象,如
str
@blacksite谢谢,我已经改变了这一点,我还添加了代码,将[替换为的[
的[
变成的[
。您的代码不会这样做。您应该避免覆盖内置对象,如str
@blacksite谢谢,我已经改变了这一点,我还添加了代码,用替换[
[在后面有一个空格。
@HarshalParekh哦,没听懂。@HarshalParekh但是因为OP没有提到我认为这是问题中的一个拼写错误。它不适用于多个。就像我想去[超级商场](地方)和[奶酪商场](地方)@MattQuaine请参见编辑。@HarshalParekh哦,没有注意到。@HarshalParekh但是因为OP没有提到我认为这是问题中的一个输入错误。它不适用于多个输入错误。就像我想去[super mall](地点)和[cheese mall](地点)@MattQuaine请参见编辑。