加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Replace Matches with Replacements Generated in Code (用函数

发布时间:2020-12-14 01:45:39 所属栏目:百科 来源:网络整理
导读:需求: 将1 and 2 and 3变成2 and 4 and 6 (倍数关系) 方法: Python def computereplacement(matchobj): return str(int(matchobj.group()) * 2) import re subject = '1 and 2 and 3' regobj = re.compile(r"d+") result = regobj.sub(computereplacement

需求:

将1 and 2 and 3变成2 and 4 and 6 (倍数关系)


方法:

Python

def computereplacement(matchobj):

return str(int(matchobj.group()) * 2)


import re

subject = '1 and 2 and 3'

regobj = re.compile(r"d+")

result = regobj.sub(computereplacement,subject)

print result


Tcl:

set pos 0 set subject "1 and 2 and 3" while {[regexp -indices -start $pos -linestop {d+} $subject offsets]==1} { set pos [expr {1+[lindex $offsets 1]}] set start [lindex $offsets 0] set end [lindex $offsets 1] set element [string range $subject [lindex $offsets 0] [lindex $offsets 1]] set subject [string replace $subject $start $end [expr $element * 2]] } puts "subject:$subject"

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读