1

My json string structure as below

...
}],"twitter":[{"id": .... blaa"}]}
...

I am trying to remove this part as below

Regex.Replace(_VarJson, string.Format("{0}.*?{1}", "\"twitter\":[{", "\"}]"), string.Empty)

But nothing removes. Where is my wrong?

Thank you in advance

3
  • Regex.Replace returns the modified string. Commented Apr 6, 2015 at 10:50
  • @npinti OK how can I do that? Could you give some sample? Commented Apr 6, 2015 at 10:53
  • Don't do Json processing with a regex, any cascading representation should be processed with context-free grammars, or use a JSON library. Commented Apr 6, 2015 at 10:56

1 Answer 1

1

In your regex pattern [{}] symbols should be escaped with \ symbol since they are reserved regex symbols ([] stands for charactrers group and {} stands for repetitions count).

So your replacement could be done as

_VarJson = Regex.Replace(_VarJson, 
    string.Format("{0}.*?{1}", 
    "\"twitter\":\\[\\{", "\"\\}\\]"), 
    string.Empty);

But I strongly agreed with opinion of @CommuSoft posted in comments - it's better to use some JSON library to parse your source JSON, then remove all you need from object model and write back JSON as text if needed.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.