我正在构建SMS网关,如果SMS消息有任何凭据信息(例如密码),我将其屏蔽,然后将其保存到数据库中。
以下是代码:
String message = "Your password is [MASK:1234]!";
boolean bMasked = message.matches("(\\[MASK:)*(\\])");
String plainText = message.replaceAll(..., "");
String withStars = message.replaceAll("...", "*");
System.out.println("bMasked: " + bMasked);
System.out.println("plainText: " + plainText);
System.out.println("withStars: " + withStar);
我对正则表达式的了解很差,所以如果可能的话,我需要你的帮助来获得以下输出:
bMasked: true
plainText: Your password is 1234!
withStars: Your password is ****!
答案 0 :(得分:1)
String message = "Your password is [MASK:1234]!";
boolean bMasked = message.matches(".*\\[MASK:[^\\]]*\\].*");
String plainText = message.replaceAll("\\[MASK:([^\\]]*)\\]", "$1");
String withStars = message.replaceAll("\\[MASK:[^\\]]*\\]", "******");
System.out.println("bMasked: " + bMasked);
System.out.println("plainText: " + plainText);
System.out.println("withStars: " + withStars);
给你:
bMasked: true
plainText: Your password is 1234!
withStars: Your password is ******!