Following string contains JSon parsed string.
以下字符串包含JSon解析的字符串。
string myJSonString="{ \"_id\" : { \"$oid\" : \"54f6b062036de221c00f5540\" }, \"account\" : \"Acc1\", \"accuser\" : \"\", \"curstatus\" : \"booked\" }";
string oldString=" { \"$oid\" :";
string newString="";
I am using below code for replacing the substring using regex but it doesn't work;
我使用下面的代码使用正则表达式替换子字符串,但它不起作用;
string result = Regex.Replace(myJSonString, oldString, newString, RegexOptions.IgnoreCase);
Second Replace
string oldStrin="}, \"account\"";
string newString=", \"account\"";
string finalResult = Regex.Replace(result , oldString2, newString2, RegexOptions.IgnoreCase);
This code doesn't work for me;
这段代码对我不起作用;
2 个解决方案
#1
1
If it's really as simple as in your example, I would use a regular string replacement, which I find much easier to understand:
如果它真的像你的例子一样简单,我会使用常规的字符串替换,我觉得更容易理解:
string myJSonString="{ \"_id\" : { \"$oid\" : \"54f6b062036de221c00f5540\" }, \"account\" : \"Acc1\", \"accuser\" : \"\", \"curstatus\" : \"booked\" }";
string result = myJSonString.Replace(" { \"$oid\" :", "").Replace("}, \"account\"", ", \"account\"");
//{ "_id" : "54f6b062036de221c00f5540" , "account" : "Acc1", "accuser" : "", "curstatus" : "booked" }
#2
0
Your version probably isn't working due to the fact that $
is a special character in regular expressions - it is an anchor, denoting the end of a line or the end of a string, so it needs to be escaped with a \
.
由于$是正则表达式中的特殊字符,因此您的版本可能无法正常工作 - 它是一个锚点,表示行的结尾或字符串的结尾,因此需要使用\来进行转义。
Having said that, rather than replacing the section before and after the ID with nothing, instead we can construct a single regex to find the entire section and replace it with just the inner ID:
话虽如此,我们可以构建一个正则表达式来查找整个部分并将其替换为内部ID,而不是在ID之前和之后替换部分。
string pattern = "{ \"\\$oid\" : (\"[a-z0-9]+\") }";
string myJSonString = "{ \"_id\" : { \"$oid\" : \"54f6b062036de221c00f5540\" }, \"account\" : \"Acc1\", \"accuser\" : \"\", \"curstatus\" : \"booked\" }";
string finalResult = Regex.Replace(myJSonString, pattern, "$1");
#1
1
If it's really as simple as in your example, I would use a regular string replacement, which I find much easier to understand:
如果它真的像你的例子一样简单,我会使用常规的字符串替换,我觉得更容易理解:
string myJSonString="{ \"_id\" : { \"$oid\" : \"54f6b062036de221c00f5540\" }, \"account\" : \"Acc1\", \"accuser\" : \"\", \"curstatus\" : \"booked\" }";
string result = myJSonString.Replace(" { \"$oid\" :", "").Replace("}, \"account\"", ", \"account\"");
//{ "_id" : "54f6b062036de221c00f5540" , "account" : "Acc1", "accuser" : "", "curstatus" : "booked" }
#2
0
Your version probably isn't working due to the fact that $
is a special character in regular expressions - it is an anchor, denoting the end of a line or the end of a string, so it needs to be escaped with a \
.
由于$是正则表达式中的特殊字符,因此您的版本可能无法正常工作 - 它是一个锚点,表示行的结尾或字符串的结尾,因此需要使用\来进行转义。
Having said that, rather than replacing the section before and after the ID with nothing, instead we can construct a single regex to find the entire section and replace it with just the inner ID:
话虽如此,我们可以构建一个正则表达式来查找整个部分并将其替换为内部ID,而不是在ID之前和之后替换部分。
string pattern = "{ \"\\$oid\" : (\"[a-z0-9]+\") }";
string myJSonString = "{ \"_id\" : { \"$oid\" : \"54f6b062036de221c00f5540\" }, \"account\" : \"Acc1\", \"accuser\" : \"\", \"curstatus\" : \"booked\" }";
string finalResult = Regex.Replace(myJSonString, pattern, "$1");