0

我正在构建 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 ****!
4

1 回答 1

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 ******!
于 2013-02-17T10:30:06.910 回答