許多實現(JavaScript,Python等)讓你指定一個函數作爲替換參數。該函數通常將整個匹配的字符串,其在輸入字符串中的位置以及捕獲的組作爲參數。該函數返回的字符串用作替換文本。
以下是如何使用JavaScript:replace函數將整個匹配的子字符串作爲其第一個參數,捕獲的組的值作爲下一個n參數,後跟原始輸入字符串中匹配的字符串的索引以及整個輸入字符串。
var s = "this is a test. and this is another one.";
console.log("replacing");
r = s.replace(/(this is) ([^.]+)/g, function(match, first, second, pos, input) {
console.log("matched :" + match);
console.log("1st group :" + first);
console.log("2nd group :" + second);
console.log("position :" + pos);
console.log("input :" + input);
return "That is " + second.toUpperCase();
});
console.log("replaced string is");
console.log(r);
輸出繼電器:
replacing
matched :this is a test
1st group :this is
2nd group :a test
pos :0
input :this is a test. and this is another one.
matched :this is another one
1st group :this is
2nd group :another one
pos :20
input :this is a test. and this is another one.
replaced string is
That is A TEST. and That is ANOTHER ONE.
這裏是Python版本 - 它甚至讓你對每個組開始/結束值:
#!/usr/bin/python
import re
s = "this is a test. and this is another one.";
print("replacing");
def repl(match):
print "matched :%s" %(match.string[match.start():match.end()])
print "1st group :%s" %(match.group(1))
print "2nd group :%s" %(match.group(2))
print "position :%d %d %d" %(match.start(), match.start(1), match.start(2))
print "input :%s" %(match.string)
return "That is %s" %(match.group(2).upper())
print "replaced string is \n%s"%(re.sub(r"(this is) ([^.]+)", repl, s))
輸出:
replacing
matched :this is a test
1st group :this is
2nd group :a test
position :0 0 8
input :this is a test. and this is another one.
matched :this is another one
1st group :this is
2nd group :another one
position :20 20 28
input :this is a test. and this is another one.
replaced string is
That is A TEST. and That is ANOTHER ONE.
標準很可能不是,你可以在一些實現中做到這一點:'$ echo testx | perl -pe's /(test)/ \ U \ 1 \'' - >'TESTx' – mykhal 2010-07-30 07:26:56
許多實現(javascript,python等)讓您指定一個函數作爲替換參數 - 該函數通常使用匹配的字符串和捕獲的組作爲參數,其返回值用作替換文本。 – Amarghosh 2010-07-30 08:35:49
@Amarghosh:你也可以將其作爲回答發佈,並在你處理時添加一些示例代碼。 – 2010-07-30 08:48:50