4

I have the following json:

Data: {"Account":"d\\adm","Password":"cWExZjEiMTM="},"SqlServer":{"InstanceName":"","MachineName":"MyMachine","Port":null}

I would like to use RegExp to replace Password value with ***.

cWExZjEiMTM= => ***

In my example I'm expecting the following output:

Data: {"Account":"d\\adm","Password":"***"},"SqlServer":{"InstanceName":"","MachineName":"MyMachine","Port":null

I only have the following solution:

string invokeSpec = "\":{\"Account\":\"d\\\\adm\",\"password\":\"cWExZjEiMTM=\"},\"SqlServer\":{\"InstanceName\":\"\",\"MachineName\":\"MyMachine\",\"Port\":null}";
var pattern = "\\\"Password\\\":\\\"(?<pass>[^\\\"]*)\\\"";
var replaced = Regex.Replace(invokeSpec, pattern, "\"Password\":\"***\"", RegexOptions.IgnoreCase); 

":{"Account":"d\\adm","Password":"***"},"SqlServer":{"InstanceName":"","MachineName":"MyMachine","Port":null}
5
  • 5
    Use a JSON parser. Don't try parsing JSON with regex. Commented Feb 28, 2020 at 14:49
  • @Amy I have to log everything before it goes to deserialization module Commented Feb 28, 2020 at 14:50
  • I don't understand what exactly is the question. It seems that you already have a working solution. So, what do you expect from a valid answer? Commented Feb 28, 2020 at 14:55
  • 1
    You can use JObject.Parse to parse it an object. Then you can try to modify it. Commented Feb 28, 2020 at 14:58
  • @Dialecticus I'm looking for replacing only password value. And now I'm replacing "Password":"Value". I mean I would like to kick "Password" from my replacement literal Commented Feb 28, 2020 at 15:03

3 Answers 3

5

I suggest to use JObject to replace the Password

var jObject = JObject.Parse(jsonString);
jObject["Password"] = "*****";
Console.WriteLine(jObject.ToString()); //save this value to log
Sign up to request clarification or add additional context in comments.

3 Comments

While this works, and even though it's clearer, it may not fit OP's performance or otherwise contextual requirements. The question asks for a Regex solution, and this is more suitable as a comment pointing towards good practices.
@Kilazur this is an answer that OP may choose over the actual answer. Krishna answered the question that OP should perhaps have asked.
@Dialecticus I do agree with the sentiment, but this is still not the question that has been asked. Moreoever, OP did comment that he needed to do it in Regex; perhaps it's actually not the case, but we don't have enough context to tell so, and to get enough context, it may requires a completely new question indeed. Then again, I have no horse in this race, just wanted to point out to SO etiquette.
2

As people said in the comment, if you can, use a JSON parser instead of regular expression. But let's dismiss that, since that's not the question.

You're capturing the wrong part of your input. What you want is a substitution.

string invokeSpec = "\":{\"Account\":\"d\\\\adm\",\"password\":\"cWExZjEiMTM=\"},\"SqlServer\":{\"InstanceName\":\"\",\"MachineName\":\"MyMachine\",\"Port\":null}";
var pattern = "(\\\"password\\\":\\\")[^\\\"]*(\\\")";
var replaced = Regex.Replace(invokeSpec, pattern, "$1***$2", RegexOptions.IgnoreCase);

As you can see, we're capturing two groups, the parts that the password is between of. So, there are 3 parts to the regular expression:

  • (\\\"password\\\":\\\") => Part to the left of the password
  • [^\\\"]* => the password itself (note that in your example, you can replace this with the non-greedy .*?)
  • (\\\") => Part to the right of the password

And we're referencing them with $1 and $2 in the replace method, replacing the original password with ***.

2 Comments

my password in base64 format so .*? does not work
Why is that @isxaker? . matches any character, *? matches until the next part of the regex is matched. base64 strings don't contain backslashes, which is the first character of the right part of the pattern.
0
String pass = "abc=gwdgjs, vhhhhycPassword.10.22.33.09.0=hsdfjs,hjghygh=uhh";
        String passKeyStar = "";
        String passKeyStar1 = "";
        String keyValue[] = pass.split(",");
        for(String t : keyValue) {
        //passKeyStar = t;
        if(t.contains("Password")){
        passKeyStar1=t.split("=")[1];
        }
        }
        System.out.println("replace text: "+pass.replaceAll(passKeyStar1, "******"));

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

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.