使用 polyethene 的grep函数,您可以给它一个正则表达式字符串,它会返回所有匹配项的分隔字符串。然后,您可以用您的字符串替换该数字的确切实例。在这个线程中(感谢 HamZa DzCyberDeV),有一个解释为什么会这样。
(为此,您需要grep 脚本!)
ReplaceNumber(whattext, instance, replacewith){
numpos := grep(whattext, "[+-]?\d+(?:\.\d+)?",thisnumber,1,0,"|")
stringsplit, numpos, numpos,|
stringsplit, thisnumber,thisnumber,|
thispos := numpos%instance% ;get the position of the capture
thisinstance := thisnumber%instance% ;get the capture itself
thislen := strlen(thisinstance)
;now fetch the string that comes before the named instance
leftstring := substr(whattext, 1, thispos-1)
rightstring := substr(whattext, thispos+thislen, strlen(whattext))
returnthis := leftstring . replacewith . rightstring
return returnthis
}
msgbox, % replacenumber("7 men swap 55.2 or 55.2 for 100 and -100.", 5, "SWAPPED")
结果:
; 1--> SWAPPED men swap 55.2 for 100 and -100.
; 2--> 7 men swap SWAPPED or 55.2 for 100 and -100.
; 3 --> 7 men swap 55.2 or SWAPPED for 100 and -100.
; 4 --> 7 men swap 55.2 or 55.2 for SWAPPED and -100.
; 5 --> 7 men swap 55.2 or 55.2 for 100 and SWAPPED.
谢谢,聚乙烯和哈姆扎!