[英]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.