Python 删除大括号之间的空格 [英] Python remove spaces in between braces
本文介绍了Python 删除大括号之间的空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 re 但我没有任何运气.我想知道如何删除大括号之间的空格.例如
我有这个字符串
我想去[超级商场](地点)
你看到[超级商场]"的空间了吗?
什么可以把这个字符串变成
我要去【超级商场】(地点)
如果我能在这方面得到任何帮助,我将不胜感激.
解决方案
我假设大括号是平衡的,不能嵌套.
<预><代码>>>>进口重新>>>s = '我想去[超级商场](地方)[真正的]'>>>re.sub('\[\s*(.*?)\s*\]', r'[\1]', s)'我想去[超级商场](地方)[真的]'<块引用>
它不适用于多个.就像我想去[超级商场](地方)和[奶酪商场](地方)
我认为是的.
<预><代码>>>>s = '我想去[超级商场](地点)和[奶酪商场](地点)'>>>re.sub('\[\s*(.*?)\s*\]', r'[\1]', s)'我想去[超级商场](地方)和[奶酪商场](地方)'I am using re but I am not having any luck. I want to know how to remove spaces in between braces. For example
I have this string
i want to go to the[ super mall ](place)
You see the the space in "[ super mall]"?
What can turn this string into
i want to go to the [super mall](place)
I would appreciate any help I can get on this thanks.
解决方案
I'm assuming braces are balanced and cannot be nested.
>>> 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]'
It doesn't work for multiple ones. like i want to go to the [ super mall ](place) and [ cheese mall ](place)
I think it does.
>>> 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)'
这篇关于Python 删除大括号之间的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文