C# 正则表达式替换所有函数
我有以下示例字符串:C# 正则表达式替换所有函数,c#,.net,regex,C#,.net,Regex,我有以下示例字符串: $0$AAAA$1$bbbbb$2$CCCCC 美元标记之间有一些数字。我想使用Regex将每个数字替换为数字+1,因此输出应该是: $1$AAAA$2$bbbbb$3$CCCC 如何使用C#中的正则表达式来实现这一点? 我知道如何找到所有数字: string s = "$0$aaaaa$1$bbbbb$2$cccccc"; Regex regex = new Regex(@"\$(?<myNumber>.*?)\$"); MatchCollection ma
$0$AAAA$1$bbbbb$2$CCCCC
美元标记之间有一些数字。我想使用Regex将每个数字替换为数字+1,因此输出应该是:
$1$AAAA$2$bbbbb$3$CCCC
如何使用C#中的正则表达式来实现这一点?我知道如何找到所有数字:
string s = "$0$aaaaa$1$bbbbb$2$cccccc";
Regex regex = new Regex(@"\$(?<myNumber>.*?)\$");
MatchCollection matches = regex.Matches(s);
foreach (Match match in matches)
{
Console.WriteLine(match.Groups["myNumber"]);
}
Console.WriteLine("\n\nFinal string = " + s);
Console.ReadKey();
string s=“$0$aaaaa$1$bbbbb$2$cccc”;
正则表达式正则表达式=新正则表达式(@“\$(?*?)\$”;
MatchCollection matches=regex.matches;
foreach(匹配中的匹配)
{
Console.WriteLine(match.Groups[“myNumber”]);
}
Console.WriteLine(“\n\n最终字符串=“+s”);
Console.ReadKey();
通过regex.Replace(…)
我可以用一个值替换它们,但我不知道如何用regex单独替换每个数字
有人知道怎么做吗
致以最诚挚的问候,Marcin
var result=Regex
.Replace(input,@)(?+1)令人印象深刻!对于任何感兴趣的人,更多关于(?)?
var result = Regex
.Replace(input, @"(?<=\$)\d+(?=\$)", m => (int.Parse(m.Value) + 1).ToString());