如何删除“[”使用 re.sub 函数从字符串中提取字符?
我想从字符串中删除“[”方括号字符。 我正在使用 re 库。 我对这个方括号“]”没有任何问题,但我对这个方括号“[”仍然有问题。
我的代码:
depth_split = ['[575,0]']
new_string = re.sub(']','',depth_split) #working
newnew_string = re.sub('[','',new_string) #not working
PS:我使用的是python。
我的输出: ['[575,0']
我期望的输出: ['575,0']
I want to remove the '[' square bracket character from a string.
I am using re library.
I had no problems with this square bracket ']' but I still having problem with this square bracket '['.
My code:
depth_split = ['[575,0]']
new_string = re.sub(']','',depth_split) #working
newnew_string = re.sub('[','',new_string) #not working
PS: I'm using python.
The output that I have : ['[575,0']
The output that I expeting : ['575,0']
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在这里不需要使用
REGEX
,因为可以使用str.replace()
轻松完成它,但是如果使用
REGEX
是必要的,请尝试:There is no need of using
regex
here since it can be done easily usingstr.replace()
:But if using
regex
is necessary try:您似乎想要的正则表达式模式是
^\[|\]$
:The regex pattern you seem to want here is
^\[|\]$
:如果支架始终处于列表中字符串的开始和结尾,则可以使用字符串切片进行以下操作:
output::
If the brackets are always at the beginning and end of the strings in your list then you can do this with a string slice as follows:
Output: