Javascript 匹配编号并在替换前添加

Javascript 匹配编号并在替换前添加,javascript,c#,regex,Javascript,C#,Regex,假设我在text.txt中有: 我希望它成为(添加9): 在javascript中,它将是: var output = input.replace(/prop:(['"])txt(\d+)\1/g, function(match, quote, number){ return "prop:" + quote + "txt" + (parseInt(number) + 9) + quote; }); 我正试图用C语言编写上述代码: Visual Studio显示第三个参数应该是Match

假设我在text.txt中有:

我希望它成为(添加9):

在javascript中,它将是:

var output = input.replace(/prop:(['"])txt(\d+)\1/g, function(match, quote, number){
    return "prop:" + quote + "txt" + (parseInt(number) + 9) + quote;
});
我正试图用C语言编写上述代码:

Visual Studio显示第三个参数应该是
MatchEvaluator-evaluator
。但我不知道如何声明/编写/使用它

欢迎任何帮助。谢谢您的时间。

您可以使用a和
Int32。Parse
将数字解析为可以添加9的int值:

Regex.Replace(content, @"prop:(['""])txt(\d+)\1", 
m => string.Format("prop:{0}txt{1}{0}",
     m.Groups[1].Value, 
    (Int32.Parse(m.Groups[2].Value) + 9).ToString()))
见:

请注意,我使用的是逐字字符串文字,以便使用单个反斜杠转义特殊字符并定义速记字符类(但是,在逐字字符串文字中,双引号必须加倍以表示单个文字双引号)。

是。您需要编写一个函数,该函数接受并返回替换值。一种方法如下所示:

private static string AddEvaluator(Match match)
{
    int newValue = Int32.Parse(match.Groups[2].Value) + 9;
    return String.Format("prop:{0}txt{1}{0}", match.Groups[1].Value, newValue)
}

public static void Main()
{
    string path = @"C:/text.txt";
    string content = File.ReadAllText(path);
    File.WriteAllText(path, Regex.Replace(content, "prop:(['\"])txt(\\d+)\\1", AddEvaluator));
}

简而言之,你想用regex?@noob做算术运算。我需要更改数值,我唯一想到的是使用正则表达式。。不管怎样,是的,你可以认为它是算术的,我把另一个标记为接受,因为它配得更好…但是也谢谢你+1。。。我想我现在明白了;)
string path = @"C:/text.txt";
string content = File.ReadAllText(path);
File.WriteAllText(path, Regex.Replace(content, "prop:(['\"])txt(\\d+)\\1", ?????));
Regex.Replace(content, @"prop:(['""])txt(\d+)\1", 
m => string.Format("prop:{0}txt{1}{0}",
     m.Groups[1].Value, 
    (Int32.Parse(m.Groups[2].Value) + 9).ToString()))
var content = "prop:\"txt1\"  prop:'txt4'  prop:\"txt13\"";
var r = Regex.Replace(content, @"prop:(['""])txt(\d+)\1", 
    m => string.Format("prop:{0}txt{1}{0}",
         m.Groups[1].Value, 
        (Int32.Parse(m.Groups[2].Value) + 9).ToString()));
Console.WriteLine(r); // => prop:"10"  prop:'13'  prop:"22" 
private static string AddEvaluator(Match match)
{
    int newValue = Int32.Parse(match.Groups[2].Value) + 9;
    return String.Format("prop:{0}txt{1}{0}", match.Groups[1].Value, newValue)
}

public static void Main()
{
    string path = @"C:/text.txt";
    string content = File.ReadAllText(path);
    File.WriteAllText(path, Regex.Replace(content, "prop:(['\"])txt(\\d+)\\1", AddEvaluator));
}