我有一个非常大的数字(它的长度可能会有所不同)作为输入。
我需要一个正则表达式,它将保留前 3 位数字和后 3 位数字不变,并将它们之间的所有数字替换为某个字符。输出的总长度应保持不变。
例如:
输入123456789123456
输出123xxxxxxxxx456
到目前为止,我能够通过使用将输入数字分成 3 组
^(\d{3})(.*)(\d{3})
第二组是需要替换的那组,所以它会是这样的
$1 {Here goes the replacement of the 2 group} $3
我正在为更换而苦苦挣扎:
Regex r = new Regex("^(\d{3})(.*)(\d{3})");
r.Replace(input,"$1 {Here goes the replacement of the 2 group} $3")
我应该如何在这里写 2 组的替换?
提前致谢。
最佳答案
你可以试试下面的正则表达式,它使用了 lookbehind 和 lookahead,
string str = "123456789123456";
string result = Regex.Replace(str, @"(?<=\d{3})\d(?=\d{3})", "x");
Console.WriteLine(result);
Console.ReadLine();
输出:
123xxxxxxxxx456
关于c# - 正则表达式:用特殊字符替换数字中间的位数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25764693/