繁体   English   中英

使用正则表达式从字符串中删除一些多余的文本

[英]Remove some extra text from a String using Regex

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM