Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Showing email address as hint

I have seen Mail services displays the email id's as e*****[email protected] , mostly in their recovery page.

So i am trying to replace the [email protected] as e*****[email protected] .

Is it possible to achieve it using String#replace(String) alone ? or should i use some REGEX to achieve it .

Thanks for your valuable suggestions in adavance

like image 219
Santhosh Avatar asked Mar 18 '23 04:03

Santhosh


2 Answers

Search regex:

\b(\w)\S*?(\S)(?=@)(\S+)\b

Replacement Pattern:

$1****$2$3****$4

RegEx Demo

Code:

String email = "[email protected]"; 
String repl = email.replaceFirst("\\b(\\w)\\S*?(\\S@)(\\S)\\S*(\\S\\.\\S*)\\b", 
      "$1****$2$3****$4");
//=> a****e@g****l.com
like image 107
anubhava Avatar answered Mar 26 '23 03:03

anubhava


You can try without regex too

 String email = "[email protected]";
 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*****[email protected]
like image 32
Ruchira Gayan Ranaweera Avatar answered Mar 26 '23 01:03

Ruchira Gayan Ranaweera