应用re.sub正显示出不寻常的行为
问题描述:
import re
value_list = ['oper-status','downward','upward','some','mid']
regex = r"\$\d+"
test_str = "Test Succeeded!! value is within the range of $1-$2 ,$3 $4 its value is {{post['x']}}"
matches = re.finditer(regex, test_str)
i = 0
if len(value_list) > 1 :
for matchNum, match in enumerate(matches):
matchNum = matchNum + 1
i = i + 1
print ("{match}".format(match = match.group()))
test_str = re.sub(regex,value_list[i],test_str,count=i)
print test_str
和我得到以下输出应用re.sub正显示出不寻常的行为
$1
$2
$3
$4
Test Succeeded!! value is within the range of downward-upward ,upward some its value is {{post['x']}}
,而我期待
Test Succeeded!! value is within the range of downward-upward ,some mid its value is {{post['x']}}
什么是我的代码
答
因为有你”的问题重新使用count=i
,您的字符串将被替换为这样:
i=1
- >"Test Succeeded!! value is within the range of downward-$2 ,$3 $4 its value is {{post['x']}}"
i=2
- >"Test Succeeded!! value is within the range of downward-upward ,upward $4 its value is {{post['x']}}"
i=3
- >"Test Succeeded!! value is within the range of downward-upward ,upward some its value is {{post['x']}}"
您应该使用count=1
代替。
答
使用直接re.sub
与正则表达式\$(\d+)
同时通过lambda表达式内第1个值转换为int
访问从列表中替换字符串:
import re
value_list = ['oper-status','downward','upward','some','mid']
regex = r"\$(\d+)"
test_str = "Test Succeeded!! value is within the range of $1-$2 ,$3 $4 its value is {{post['x']}}"
print re.sub(regex,lambda x: value_list[int(x.group(1))], test_str)
这一个通过正则表达式替换将帮助你避免像你原来的方法一样的问题,即你用count=i
替换了超过必要的次数。
如果能像$12
比赛(和列表没有12种元素)添加一个条件:
print(re.sub(regex,lambda x: value_list[int(x.group(1))] if int(x.group(1)) < len(value_list) else x.group(), test_str))
答
它,因为你在re.sub
count=i
代替
count=1
- 在第1次迭代中,i = 1,$ 1被替换
- 在第二次迭代,I = 2,$ 2 $ 3在第3次迭代更换
- ,I = 3,$ 4在第四次迭代更换
- ,i = 4的,没有什么可以替代
你可以做这一切使用str.format
value_list = ['oper-status','downward','upward','some','mid']
test_str = "Test Succeeded!! value is within the range of {x[1]}-{x[2]} ,{x[3]} {x[4]} its value is {{post['x']}}".format(x=value_list)
# "Test Succeeded!! value is within the range of downward-upward ,some mid its value is {post['x']}"
@WiktorStribiżew所以如何将我达到我的预期输出,如果你能 –
你只是需要直接使用're.sub'指出,通过比赛没有点迭代。 –