[英]Showing email address as hint
I have seen Mail services displays the email id's as e*****e@gmail.com
, mostly in their recovery page. 我看过邮件服务显示电子邮件ID为
e*****e@gmail.com
,主要是在他们的恢复页面中。
So i am trying to replace the example@gmail.com
as e*****e@gmail.com
. 所以我试图将
example@gmail.com
替换为e*****e@gmail.com
。
Is it possible to achieve it using String#replace(String)
alone ? 是否可以单独使用
String#replace(String)
来实现它? or should i use some REGEX to achieve it . 或者我应该使用一些REGEX来实现它。
Thanks for your valuable suggestions in adavance 感谢您提供宝贵的建议
Search regex: 搜索正则表达式:
\b(\w)\S*?(\S)(?=@)(\S+)\b
Replacement Pattern: 替换模式:
$1****$2$3****$4
Code: 码:
String email = "anexample@gmail.com";
String repl = email.replaceFirst("\\b(\\w)\\S*?(\\S@)(\\S)\\S*(\\S\\.\\S*)\\b",
"$1****$2$3****$4");
//=> a****e@g****l.com
It could be possible through replaceAll function. 它可以通过replaceAll函数实现。
(?<!^).(?=.*?.@)
Use the above regex and replace the matched characters with *
使用上面的正则表达式并将匹配的字符替换为
*
String s = "example@gmail.com";
System.out.println(s.replaceAll("(?<!^).(?=.*?.@)", "*"));
Output: 输出:
e*****e@gmail.com
Update: 更新:
Use the below regex to get the output like e*****e@g***l.com
使用以下正则表达式获取输出,如
e*****e@g***l.com
String s = "example@gmail.com";
System.out.println(s.replaceAll("\\B.\\B(?=.*?\\.)", "*"));
Output: 输出:
e*****e@g***l.com
You can try without regex too 你也可以尝试没有正则表达式
String email = "example@gmail.com";
int start = 1;
int end = email.indexOf("@") - 1;
StringBuilder sb = new StringBuilder(email);
StringBuilder sb1=new StringBuilder();
for(int i=start;i<end;i++){
sb1.append("*");
}
sb.replace(start, end, sb1.toString());
System.out.println(sb.toString());
Out put: 出局:
e*****e@gmail.com
I sugges to use indexOf
and substring
. 我建议使用
indexOf
和substring
。 With replace you can run into tuble with emails like gmail@gmail.com
使用替换,您可以使用gmail@gmail.com等电子邮件进行
gmail@gmail.com
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.