2016-04-01 26 views
1

是否有可能替換查找-pwd的出現並用****替換引號中的立即值。使用正則表達式找到一個鍵並替換它的值

string s = "-user 'username' -locale 'us' -pwd 'realpwd' -time 'pst' "; 

string result = "-user 'username' -locale 'us' -pwd '****' -time 'pst' "; 
+2

的第一組捕獲使用正則表達式組'-pwd + \ '(。*?)\''並替換第一個捕獲g與'****'組合 – rock321987

回答

0

不使用捕獲組的替代解決方案。

您可以簡單搜索-pwd '.*?'並替換爲-pwd '****'Regex101 Demo

1

您可以使用此正則表達式來捕捉-pwd

(-pwd +)\'(?:.*?)\' 

代碼

string input = "-user 'username' -locale 'us' -pwd 'realpwd' -time 'pst' "; 
string pattern = "(-pwd +)\'(?:.*?)\'"; 
Regex rgx = new Regex(pattern); 
string result = rgx.Replace(input, "$1\'****\'"); 

Console.WriteLine("Original String: {0}", input); 
Console.WriteLine("Replacement String: {0}", result);  
Console.ReadKey(); 

IDEONE DEMO