如何替换长字符串文本中的特定字符串

时间:2017-07-26 16:25:44

标签: c# string c#-4.0 text jwt

在下面的表达中,我只需要替换 " pmcKey"字段值与另一个字符串。 " pmcKey":"值....长" ,需要替换"价值......长期"与其他字符串。 如何做到这一点,通过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}

2 个答案:

答案 0 :(得分: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)}";

答案 1 :(得分: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\"");

        }
    }

}