2010-07-30 96 views
1

有没有办法修改反向引用的值?正则表达式修改反向引用的值

示例: 在下面的文本

"this is a test" 

单词“测试”应该提取并插入到经由backrefrence另一文本。

正则表达式:

(test) 

更换:

"this is another \1" 

,到目前为止,工作正常。但现在的问题是,如果可以在插入前修改反向引用。就像将单词“test”转换为大写字母一样。

我觉得它可能看起来像:

"this is another \to_upper\1" 

有没有在“标准”中定义的东西(有没有什么标准可言?)正则表达式?

+2

标准很可能不是,你可以在一些实现中做到这一点:'$ echo testx | perl -pe's /(test)/ \ U \ 1 \'' - >'TESTx' – mykhal 2010-07-30 07:26:56

+2

许多实现(javascript,python等)让您指定一个函数作为替换参数 - 该函数通常使用匹配的字符串和捕获的组作为参数,其返回值用作替换文本。 – Amarghosh 2010-07-30 08:35:49

+0

@Amarghosh:你也可以将其作为回答发布,并在你处理时添加一些示例代码。 – 2010-07-30 08:48:50

回答

4

许多实现(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.