2016-01-21 119 views
0
string message = "Your purchase of $ 100.00 is awaiting for confirmation. Please use PIN 8967 to complete the transaction. Reference Number :1237689"; 

string output = Regex.Replace(message, @"[\d-]", "*"); 

Console.WriteLine("Message : " + message); 

Console.WriteLine("Output : " + output); 

Console.ReadLine(); 

我想只替换消息的PIN。但上面的消息从*字符替换所有数值。C#替换部分消息

+2

使用表达式'(PIN)\ d +'和替换用1'$ ****'。 – Tushar

回答

3
string message = "Your purchase of $ 100.00 is awaiting for confirmation. Please use PIN 8967 to complete the transaction. Reference Number :1237689"; 
string output = Regex.Replace(message, @"(?<=PIN\s*\d*)\d", "*"); 

string message = "Your purchase of $ 100.00 is awaiting for confirmation. Please use PIN 8967 to complete the transaction. Reference Number :1237689"; 
string output = Regex.Replace(message, @"(?<=PIN\s*)\d+", m => new string('*', m.Length)); 
+0

嗨Ulugbek,感谢您的指导。它很好用。 –

+0

@DreamCoder如果解决了您的问题,请考虑upvoting并将答案标记为已接受。谢谢! – Rob

+0

您好我在上面的for循环中替换。所有的消息都是不同的格式。如果像下面这样的消息不能替换*字符。字符串消息=“您的PIN码已被重置,新的PIN码347689登录。”;所以我需要针对这两种情况的解决方案。 –