在java中屏蔽电子邮件地址 [英] masking of email address in java

查看:168
本文介绍了在java中屏蔽电子邮件地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用*掩盖电子邮件地址,但我在正则表达式上表现不佳。

I am trying to mask email address with "*" but I am bad at regex.

input : nileshxyzae@gmail.com
output : nil********@gmail.com

我的代码是

String maskedEmail = email.replaceAll("(?<=.{3}).(?=[^@]*?.@)", "*");

但它给我输出 nil ******* e @ gmail.com 我不知道这里出了什么问题。为什么最后一个角色没有转换?
也可以有人解释所有这些正则表达式的含义

but its giving me output nil*******e@gmail.com I am not getting whats getting wrong here. Why last character is not converted? Also can someone explain meaning all these regex

推荐答案

你的预测(?= [^ @] *?。@)要求 @ 前面至少有一个字符(参见<$ c $之前的点) c> @ )。

Your look-ahead (?=[^@]*?.@) requires at least 1 character to be there in front of @ (see the dot before @).

如果删除它,您将获得所有预期的符号替换:

If you remove it, you will get all the expected symbols replaced:

(?<=.{3}).(?=[^@]*?@)

这是正则表达式演示 (替换为 * )。

但是,正则表达式不是该任务的正确正则表达式。你需要一个正则表达式,匹配前3个字符后的每个字符,直到第一个 @

However, the regex is not a proper regex for the task. You need a regex that will match each character after the first 3 characters up to the first @:

(^[^@]{3}|(?!^)\G)[^@]

请参阅另一个正则表达式演示,替换为 $ 1 * 。在这里, [^ @] 匹配任何不是 @ 的字符,因此我们不匹配<$ c等地址$ C> abc@example.com 。只有那些在用户名部分中有4个以上字符的电子邮件才会被屏蔽。

See another regex demo, replace with $1*. Here, [^@] matches any character that is not @, so we do not match addresses like abc@example.com. Only those emails will be masked that have 4+ characters in the username part.

参见 IDEONE演示

String s = "nileshkemse@gmail.com";
System.out.println(s.replaceAll("(^[^@]{3}|(?!^)\\G)[^@]", "$1*"));

这篇关于在java中屏蔽电子邮件地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆