英文:
Java regex find substring in string
问题
我在应用程序启动后打印属性。我需要隐藏密码的值。我添加了一个新的属性,如下:
secrets:my.first.password,my.second.password
并且在打印之前检查每个属性。如果secrets
包含该键,我将值替换为***。例如,我有以下这些属性:
my.first.name:一些名称
my.first.password:123
my.second.name:第二个名称
my.second.password:456
secrets:my.first.password,my.second.password
我将打印出以下行:
my.first.name:一些名称
my.first.password:***
my.second.name:第二个名称
my.second.password:***
secrets:my.first.password,my.second.password
我使用以下代码实现(protectedProperties = 分割的secrets属性):
protected String getViewableValue(String key, Object value) {
if (protectedProperties.contains(key)) {
return key + ":***";
} else {
return key + ":" + value;
}
}
现在我需要添加一个新选项。我不仅需要隐藏属性的完整键,还需要隐藏部分匹配。例如:
secrets:my.first.password,*password*
这意味着所有键中包含password关键字的属性都应该被隐藏。我编写了以下代码,它可以工作(我在单词开头和结尾处去除了*):
protected String getViewableValue(String key, Object value) {
for (String property : protectedProperties) {
if (key.contains(property)) return key + ":***";
}
return key + ":" + value;
}
但我不喜欢这个解决方案。我正在考虑使用正则表达式。我能把我的解决方案改成正则表达式吗?
英文:
I print properties after the application starts up. And I need to hide the values of passwords. I added a new property like:
secrets:my.first.password,my.second.password
And check each property before printing. If secrets
contains the key, I replace value to ***. For Example, I have these properties:
my.first.name:some name
my.first.password:123
my.second.name:second name
my.second.password:456
secrets:my.first.password,my.second.password
I will print the next lines:
my.first.name:some name
my.first.password:***
my.second.name:second name
my.second.password:***
secrets:my.first.password,my.second.password
I do it with this code(protectedProperties = splitted secrets property):
protected String getViewableValue(String key, Object value) {
if (protectedProperties.contains(key)) {
return key + ":***";
} else {
return key + ":" + value;
}
}
Now I need to add a new option. I need to hide not only the full key of the property but mask. For example:
secrets:my.first.password,*password*
It means that all properties with key contain password word should be hidden. I wrote this code and it works(I remove * from start and end of word before):
protected String getViewableValue(String key, Object value) {
for (String property : protectedProperties) {
if (key.contains(property)) return key + ":***";
}
return key + ":" + value;
}
But I do not like this solution. I am thinking about regex. Can I change my solution to regex?
答案1
得分: 1
public static void main(String[] args) {
String input = "my.first.name:some name\n"
+ "my.first.password:123\n"
+ "my.second.name:second name\n"
+ "my.second.password:456\n"
+ "secrets:my.first.password,my.second.password\n";
String result = input
.replaceAll("(my\\.(?:first|second)\\.password:)\\S+", "$1:***");
System.out.println(result);
}
Output:
my.first.name:some name
my.first.password:***
my.second.name:second name
my.second.password:***
secrets:my.first.password,my.second.password
英文:
Replace All with Regex - substitute with Variable and Substring
The Regex:
"my\\.(?:first|second)\\.password:)\\S+"
Regex in context:
public static void main(String[] args) {
String input = "my.first.name:some name\n"
+ "my.first.password:123\n"
+ "my.second.name:second name\n"
+ "my.second.password:456\n"
+ "secrets:my.first.password,my.second.password\n";
String result = input
.replaceAll("(my\\.(?:first|second)\\.password:)\\S+","$1:***");
System.out.println(result);
}
Output:
my.first.name:some name
my.first.password::***
my.second.name:second name
my.second.password::***
secrets:my.first.password,my.second.password
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论