繁体   English   中英

如何用正斜杠将数字替换为 python 中的方括号

[英]How to replace the number with forward slash to square square brackets in python

input = '/topology/1/deviceGroup/1/ethernet/1/ipv4/1'
output = '/topology[1]/deviceGroup[1]/ethernet[1]/ipv4[1]'

output = input.replace('\/[0-9]\/ + \/[0-9]', [[^0-9]])

但我没有得到确切的正则表达式来在 python 中进行替换,请帮忙。

尝试这个:

>>> import re
>>> inp = '/topology/1/deviceGroup/1/ethernet/1/ipv4/1'
>>> re.sub(r'/(\d+)', r'[\1]', inp)
'/topology[1]/deviceGroup[1]/ethernet[1]/ipv4[1]'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM