java 在java中屏蔽电子邮件地址
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33100298/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
masking of email address in java
提问by nilesh
I am trying to mask email address with "*" but I am bad at regex.
我试图用“*”屏蔽电子邮件地址,但我不擅长正则表达式。
input : [email protected]
output : nil********@gmail.com
My code is
我的代码是
String maskedEmail = email.replaceAll("(?<=.{3}).(?=[^@]*?.@)", "*");
but its giving me output nil*******[email protected]
I am not getting whats getting wrong here. Why last character is not converted?
Also can someone explain meaning all these regex
但它给了我输出nil*******[email protected]
我没有得到什么在这里出错。为什么最后一个字符没有转换?也有人可以解释所有这些正则表达式的含义
回答by Wiktor Stribi?ew
Your look-ahead (?=[^@]*?.@)
requires at least 1 character to be there in front of @
(see the dot before @
).
您的前瞻(?=[^@]*?.@)
要求前面至少有 1 个字符@
(请参阅 之前的点@
)。
If you remove it, you will get all the expected symbols replaced:
如果删除它,您将替换所有预期的符号:
(?<=.{3}).(?=[^@]*?@)
Here is the regex demo(replace with *
).
这是正则表达式演示(替换为*
)。
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 个字符到第一个字符之后的每个字符@
:
(^[^@]{3}|(?!^)\G)[^@]
See another regex demo, replace with $1*
. Here, [^@]
matches any character that is not @
, so we do not match addresses like [email protected]
. Only those emails will be masked that have 4+ characters in the username part.
参见另一个正则表达式演示,替换为$1*
. 在这里,[^@]
匹配任何不是 的字符@
,所以我们不匹配像[email protected]
. 只有在用户名部分包含 4 个以上字符的电子邮件才会被屏蔽。
See IDEONE demo:
见IDEONE演示:
String s = "[email protected]";
System.out.println(s.replaceAll("(^[^@]{3}|(?!^)\G)[^@]", "*"));
回答by Andy Turner
If you're bad at regular expressions, don't use them :) I don't know if you've ever heard the quote:
如果您不擅长正则表达式,请不要使用它们 :) 我不知道您是否听说过以下引用:
Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
有些人在遇到问题时会想“我知道,我会使用正则表达式”。现在他们有两个问题。
(source)
(来源)
You might get a working regular expression here, but will you understand it today? tomorrow? in six months' time? And will your colleagues?
您可能会在这里得到一个有效的正则表达式,但今天您能理解它吗?明天?六个月后?你的同事会吗?
An easy alternative is using a StringBuilder
, and I'd argue that it's a lot more straightforward to understand what is going on here:
一个简单的替代方法是使用 a StringBuilder
,我认为理解这里发生的事情要简单得多:
StringBuilder sb = new StringBuilder(email);
for (int i = 3; i < sb.length() && sb.charAt(i) != '@'; ++i) {
sb.setCharAt(i, '*');
}
email = sb.toString();
"Starting at the third character, replace the characters with a *
until you reach the end of the string or @
."
“从第三个字符开始,用 a 替换字符,*
直到到达字符串的末尾或@
。”
(You don't even need to use StringBuilder
: you could simply manipulate the elements of email.toCharArray()
, then construct a new string at the end).
(您甚至不需要使用StringBuilder
:您可以简单地操作 的元素email.toCharArray()
,然后在最后构造一个新字符串)。
Of course, this doesn't work correctly for email addresses where the local part is shorter than 3 characters - it would actually then mask the domain.
当然,这对于本地部分短于 3 个字符的电子邮件地址不起作用 - 它实际上会屏蔽域。
回答by TheLostMind
Your Look-ahead is kind of complicated. Try this code :
你的前瞻有点复杂。试试这个代码:
public static void main(String... args) throws Exception {
String s = "[email protected]";
s= s.replaceAll("(?<=.{3}).(?=.*@)", "*");
System.out.println(s);
}
O/P :
开/关:
nil********@gmail.com
回答by user1079877
I like this one because I just want to hide 4 characters, it also dynamically decrease the hidden chars to 2 if the email address is too short:
我喜欢这个是因为我只想隐藏 4 个字符,如果电子邮件地址太短,它还会动态地将隐藏字符减少到 2 个:
public static String maskEmailAddress(final String email) {
final String mask = "*****";
final int at = email.indexOf("@");
if (at > 2) {
final int maskLen = Math.min(Math.max(at / 2, 2), 4);
final int start = (at - maskLen) / 2;
return email.substring(0, start) + mask.substring(0, maskLen) + email.substring(start + maskLen);
}
return email;
}
Sample outputs:
示例输出:
[email protected] > my****[email protected]
[email protected] > i**[email protected]
回答by Ajay Prajapati
//In Kotlin
val email = "[email protected]"
val maskedEmail = email.replace(Regex("(?<=.{3}).(?=.*@)"), "*")
回答by Deep
public static string GetMaskedEmail(string emailAddress)
{
string _emailToMask = emailAddress;
try
{
if (!string.IsNullOrEmpty(emailAddress))
{
var _splitEmail = emailAddress.Split(Char.Parse("@"));
var _user = _splitEmail[0];
var _domain = _splitEmail[1];
if (_user.Length > 3)
{
var _maskedUser = _user.Substring(0, 3) + new String(Char.Parse("*"), _user.Length - 3);
_emailToMask = _maskedUser + "@" + _domain;
}
else
{
_emailToMask = new String(Char.Parse("*"), _user.Length) + "@" + _domain;
}
}
}
catch (Exception) { }
return _emailToMask;
}