Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
RegEx修改Backreference的值_Regex_Backreference - Fatal编程技术网

RegEx修改Backreference的值

RegEx修改Backreference的值,regex,backreference,Regex,Backreference,有没有办法修改反向引用的值 例如: 在下文中 "this is a test" 单词“test”应该被提取出来,并通过反引用插入到另一个文本中 正则表达式: (test) 替换: "this is another \1" 到目前为止效果不错。但现在的问题是,是否可以在插入之前修改反向引用。类似于将单词test转换为大写 我想它可能看起来像: "this is another \to_upper\1" 标准中有什么规定吗?有什么标准吗?正则表达式?许多javascript、python等实

有没有办法修改反向引用的值

例如: 在下文中

"this is a test"
单词“test”应该被提取出来,并通过反引用插入到另一个文本中

正则表达式:

(test)
替换:

"this is another \1"
到目前为止效果不错。但现在的问题是,是否可以在插入之前修改反向引用。类似于将单词test转换为大写

我想它可能看起来像:

"this is another \to_upper\1"

标准中有什么规定吗?有什么标准吗?正则表达式?

许多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.

许多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/'->TESTxMany实现javascript,python etc允许您指定一个函数作为替换参数-该函数通常将匹配的字符串和捕获的组作为参数,其返回值用作替换文本。@Amarghosh:您不妨将其作为答案发布,并在编写过程中添加一些示例代码。@Alan he还没有指定语言。@Amarghosh:所以请使用您最喜欢的语言。错误语言的示例比没有示例要好得多。标准很可能不是,但您可以在某些实现中实现:$echo testx | perl-pe's/test/\U\1/'->TESTxMany实现javascript,python etc允许您指定一个函数作为替换参数-该函数通常将匹配的字符串和捕获的组作为参数,其返回值用作替换文本。@Amarghosh:您不妨将其作为答案发布,并在编写过程中添加一些示例代码。@Alan he还没有指定语言。@Amarghosh:所以请使用您最喜欢的语言。一个错误语言的例子比没有好得多。9年后仍然是黄金。9年后仍然是黄金。