简体   繁体   中英

Find occurrences of string and replace using C#

I have a huge string in json format.

the problem is it is not well formatted.

What i want to do is to find occurrences of some tags and replace it's values with some other values

What i want to search for is this string "distanceP":\\somtext, and "distance":\\sometext,

i want these two string to be replace with "distanceP":"\\somtext", and "distance":"\\sometext",

The string could be LIKE "distanceP":\ᘳ, OR "distance":\\u16asa3, OR "distanceP":\ᘳsass,

Please not the quotation marks and back-slash signs are included.

Can anyone please tell me REGEXP expression to search and replace with my own values.

Have you tried string.Replace("\\"distanceP\\":\\\\", "\\"distance\\":\\\\") ? The \\ in the string escapes the next character, in effect forcing it to be part of the string as a literal. So \\\\ turns into \\ in the string. And Replace will do what you're looking for, replace a given string with another string. I don't think you need RegEx for this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM