2017-07-26 69 views
0

在下面的表達式中,我只需要用另一個字符串替換 「pmcKey」字段值。 「pmcKey」:「value .... long」,需要用其他字符串替換「value .... long」。 如何做到這一點,通過string.replace走很長的路。如何替換長字符串文本中的特定字符串

{"rd":"1404900091","d":"25994","dddd":99,"pmcKey":"95abcdefgiJBMjU2R0NNS1ciLCJlbmMiOiJBMjU2Q0JDLUhTNTEyIiwiaXYiOiJBQVNNa0lsQ3l6blRsWktiIiwidGFnIjoiUlFuNUhVSWVHMVN0TmxJdXB5SGZNZyIsInppcCI6IkRFRiJ9.6zef568_zt8uZGlOdZZZga0FJV1CJcf-RdEIUk88ZtNyEmVX7eLnuce1nhkROgA03444LRiOxkFLFJ_eW5Um8w.k72DtsRbZzuTqWqOFlacVw.D3Sn9jiKRosZboqE0v999htZuyHu4Eukcq64Df5ga6XEOIOj6vDwR-2_NxzYs58kWpvP999SsXdYfqn1m9--h3lgcJEqOb2z4u_yXzxRWsGQe8kNwdWndFJox699999lQI0djiYAQtkhgqI6hgBS_muWiYar9WpP6K3fxPn99999cXlN6L0RdqWIl_U-wV5mlpMivxfyk0fMVcD1T9GTk99999aHpPPAYJ0pHIOYJjak2tj7J_nK4jPxrw7pNbQ3h2TB71JE5UTs4P9NgsL299999eb2wdJuOgJR9md-8PiGAJvWpgQSQu9HNvGowaTq.9999999991gjk0SQEXxfFBUTJKDANTdVBN52FURbIXQ","id":null,"req":null,"page":1,"CDate":null} 
+6

鑑於這是JSON,我會將它加載爲JSON(例如,使用'JObject.Parse'),將其修改爲JSON,然後轉換回字符串。一般來說,最好儘早將數據轉換爲數據的自然域。 –

回答

2

假設你的輸入是有效的JSON你可以試試這個代碼:如果我的假設是錯誤的,你可以使用正則表達式這樣

var input = "{\"rd\":\"1404900091\",\"d\":\"25994\",\"dddd\":99,\"pmcKey\":\"95abcdefgiJBMjU2R0NNS1ciLCJlbmMiOiJBMjU2Q0JDLUhTNTEyIiwiaXYiOiJBQVNNa0lsQ3l6blRsWktiIiwidGFnIjoiUlFuNUhVSWVHMVN0TmxJdXB5SGZNZyIsInppcCI6IkRFRiJ9.6zef568_zt8uZGlOdZZZga0FJV1CJcf-RdEIUk88ZtNyEmVX7eLnuce1nhkROgA03444LRiOxkFLFJ_eW5Um8w.k72DtsRbZzuTqWqOFlacVw.D3Sn9jiKRosZboqE0v999htZuyHu4Eukcq64Df5ga6XEOIOj6vDwR-2_NxzYs58kWpvP999SsXdYfqn1m9--h3lgcJEqOb2z4u_yXzxRWsGQe8kNwdWndFJox699999lQI0djiYAQtkhgqI6hgBS_muWiYar9WpP6K3fxPn99999cXlN6L0RdqWIl_U-wV5mlpMivxfyk0fMVcD1T9GTk99999aHpPPAYJ0pHIOYJjak2tj7J_nK4jPxrw7pNbQ3h2TB71JE5UTs4P9NgsL299999eb2wdJuOgJR9md-8PiGAJvWpgQSQu9HNvGowaTq.9999999991gjk0SQEXxfFBUTJKDANTdVBN52FURbIXQ\",\"id\":null,\"req\":null,\"page\":1,\"CDate\":null}"; 
var json = JObject.Parse(input); 
json["pmcKey"] = "other string"; 
var result = json.ToString(); 


編輯

var regex = new Regex("\"pmcKey\":\"(?<oldValue>[^\"]+)\""); 
var oldValue = regex.Match(input).Groups["oldValue"]; 
var newValue = "otherValue"; 
var result = $"{input.Substring(0, oldValue.Index)}{newValue}{input.Substring(oldValue.Index + oldValue.Length)}"; 
+0

謝謝。 JObject的作品。實際上,正則表達式不起作用,'oldvalue'可以是任何東西。 – user3711357

0

嘗試正則表達式:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Text.RegularExpressions; 


namespace ConsoleApplication68 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      string input = "{\"rd\":\"1404900091\",\"d\":\"25994\",\"dddd\":99,\"pmcKey\":\"95abcdefgiJBMjU2R0NNS1ciLCJlbmMiOiJBMjU2Q0JDLUhTNTEyIiwiaXYiOiJBQVNNa0lsQ3l6blRsWktiIiwidGFnIjoiUlFuNUhVSWVHMVN0TmxJdXB5SGZNZyIsInppcCI6IkRFRiJ9.6zef568_zt8uZGlOdZZZga0FJV1CJcf-RdEIUk88ZtNyEmVX7eLnuce1nhkROgA03444LRiOxkFLFJ_eW5Um8w.k72DtsRbZzuTqWqOFlacVw.D3Sn9jiKRosZboqE0v999htZuyHu4Eukcq64Df5ga6XEOIOj6vDwR-2_NxzYs58kWpvP999SsXdYfqn1m9--h3lgcJEqOb2z4u_yXzxRWsGQe8kNwdWndFJox699999lQI0djiYAQtkhgqI6hgBS_muWiYar9WpP6K3fxPn99999cXlN6L0RdqWIl_U-wV5mlpMivxfyk0fMVcD1T9GTk99999aHpPPAYJ0pHIOYJjak2tj7J_nK4jPxrw7pNbQ3h2TB71JE5UTs4P9NgsL299999eb2wdJuOgJR9md-8PiGAJvWpgQSQu9HNvGowaTq.9999999991gjk0SQEXxfFBUTJKDANTdVBN52FURbIXQ\",\"id\":null,\"req\":null,\"page\":1,\"CDate\":null}"; 
      string pattern = "\"pmcKey\":\"[^\"]+\""; 

      string output = Regex.Replace(input, pattern, "pmc\":\"abc\""); 

     } 
    } 

} 
相關問題