S O
S O

Reputation: 211

what is the regular expression for this?

I need to mask the password before it gets displayed in the log file.

the format of the password is "password":"pswd123". it's alphanumeric only. After masking, it'd be "password":"*"

in my custom Pattern class, I've the following reg expression but it's not being picked up. any idea how it should be? thx

@Override
public String format(LoggingEvent event) {

    String msg = super.format(event);

    // regexp not being picked up
    msg = msg.replace("\"password\":\"[^\"]*", "password:\"***\"");

    return msg;
}

Upvotes: 2

Views: 954

Answers (5)

Vineet Bhatia
Vineet Bhatia

Reputation: 2533

Are you making use of any logging library like log4j or slf4j? These libraries have features to "replace strings" using regular expressions. You can use and apply this globally by changing in logging configuration file. You would still need to come up with a regular expression for which use a regular expression builder utility such as http://myregexp.com/ and build a regular expression on your own.

Upvotes: 0

Nick
Nick

Reputation: 4766

If you want it to just show 3 * that's easy, if you want it to show 1 * for each character in the password, that's a little harder.

Msg = Regex.Replace(Msg, "\"Password\":\"[^\"]+?\"", "\"Password\":\"***\"")

Upvotes: 0

ptyx
ptyx

Reputation: 4164

  1. replaceAll is what you're looking for
  2. If you want a full match, you are missing the last \" at the end of the regexp

Upvotes: 0

fge
fge

Reputation: 121712

Use .replaceFirst(), .replace() only replaces substrings

Upvotes: 0

NPE
NPE

Reputation: 500227

String.replace() takes a CharSequence, not a regex. You're probably looking for replaceAll() or replaceFirst().

Upvotes: 4

Related Questions