1

I have a string some thing like this:

If message contains sensitive info like: {Password:123456, tmpPwd : tesgjadgj, TEMP_PASSWORD: kfnda}

My pattern should look for the particular words Password or tmpPwd or TEMP_PASSWORD.

How can I create a pattern for this kind of search?

2
  • (Password|tmpPwd|TEMP_PASSWORD) Commented Jun 8, 2015 at 12:40
  • Thank you Karthik. I am looking for this solution only Commented Jun 8, 2015 at 12:57

2 Answers 2

1

I think you are looking for the values after these words. You need to set capturing groups to extract those values, e.g.

String content = "If message contains sensitive info like: {Password:123456, tmpPwd : tesgjadgj, TEMP_PASSWORD: kfnda} ";
Pattern p = Pattern.compile("\\{Password\\s*:\\s*([^,]+)\\s*,\\s*tmpPwd\\s*:\\s*([^,]+)\\s*,\\s*TEMP_PASSWORD:\\s*([^,]+)\\s*\\}");
Matcher m = p.matcher(content);
while (m.find()) {
  System.out.println(m.group(1) + ", " + m.group(2) + ", " + m.group(3));
}

See IDEONE demo

This will output 123456, tesgjadgj, kfnda.

To just find out if there are any of the substrings, use contains method:

System.out.println(content.contains("Password") || 
                       content.contains("tmpPwd") ||
                       content.contains("TEMP_PASSWORD"));

See another demo

And if you want a regex-solution for the keywords, here it is:

String str = "If message contains sensitive info like: {Password:123456, tmpPwd : tesgjadgj, TEMP_PASSWORD: kfnda} ";
Pattern ptrn = Pattern.compile("Password|tmpPwd|TEMP_PASSWORD");
Matcher m = ptrn.matcher(str);
while (m.find()) {
   System.out.println("Match found: " + m.group(0));
}

See Demo 3

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

6 Comments

Thank you I am looking for keywords
But then you do not need a regex, just contains is enough. I added another snippet showing how it can be done without regex. Surely, you can use alternation in regex, but unless you need to check for whole words, the contains method is sufficient.
Yes , What you said is correct , contains will be fine , but I thought using regex will be more efficient than usual string comparison, creating array of words looping them and searching for them in the msg. I am trying to use this code masking message at logging. If the message contains above sensitive words I will not log the message . I tried to look into log4j config but I couldnt find any , so thought of using regex
Ok, I see, but in 99.9% of cases when you do not have to use a regex, you should not use regex, as it is slowing down your application much more than using string functions.
Thank you . Finally I am using it like as per my requirement . private final static String censoredWords = "PASSWORD|Password|password|Pwd|pwd|PWD"; But is there anyway to update my pattern , to avoid writing words in all cases
|
1

Finally I am using it like as per my requirement .

private final static String censoredWords = "(?i)PASSWORD|pwd";

The (?i) makes it case-insensitive

1 Comment

Thank you ! This answer is useful to me

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.