电子邮件屏蔽的正则表达式 [英] Regular expression for email masking
问题描述
我正在尝试编写正则表达式来屏蔽电子邮件地址.下面的示例.
I am trying to write a regular expression to mask an email address. Example below.
输入:john.doe@example.en.com
input: john.doe@example.en.com
输出:j ******* @ e *********.com
output: j*******@e*********.com
我尝试了以下方法,但似乎无法正常工作.
I have tried the following but I just can't seem to get it working correctly.
regex:
(?<=.).(?=[^@]\*?@)
输出:j ******* @ example.en.com
output:j*******@example.en.com
regex:(?<=.).(?=[^@]\*?)(?=[^\.]\*?\.)
output:j ******************.com
output:j******************.com
任何帮助将不胜感激. 演示
Any help would be appreciated. demo
推荐答案
使用各种屏蔽电子邮件解决方案进行更新
-
foo@bar.com
⇒f**@b**.com
(当前问题)-s.replaceAll("(?<=.)[^@](?=[^@]*?@)|(?:(?<=@.)|(?!^)\\G(?=[^@]*$)).(?=.*\\.)", "*")
(请参见正则表达式演示)Update with various masking email solutions
foo@bar.com
⇒f**@b**.com
(current question) -s.replaceAll("(?<=.)[^@](?=[^@]*?@)|(?:(?<=@.)|(?!^)\\G(?=[^@]*$)).(?=.*\\.)", "*")
(see the regex demo)foo@bar.com
⇒f**@b*r.com
-s.replaceAll("(?<=.)[^@](?=[^@]*?@)|(?:(?<=@.)|(?!^)\\G(?=[^@]*$)).(?=.*[^@]\\.)", "*")
(请参见 regex演示)foo@bar.com
⇒f*o@b*r.com
-s.replaceAll("(?<=.)[^@](?=[^@]*?[^@]@)|(?:(?<=@.)|(?!^)\\G(?=[^@]*$)).(?=.*[^@]\\.)", "*")
(请参见 regex演示)foo@bar.com
⇒f**@b*****m
-s.replaceAll("(?<=.)[^@](?=[^@]*?@)|(?:(?<=@.)|(?!^)\\G(?=[^@]*$)).(?!$)", "*")
(请参见 regex演示)foo@bar.com
⇒f*o@b*****m
-s.replaceAll("(?<=.)[^@](?=[^@]*[^@]@)|(?:(?<=@.)|(?!^)\\G(?=[^@]*$)).(?!$)", "*")
(请参见 regex演示)如果您不能使用基于基于代码的解决方案,则可以使用
In case you can't use a code-based solution, you may use
s.replaceAll("(?<=.)[^@](?=[^@]*?@)|(?:(?<=@.)|(?!^)\\G(?=[^@]*$)).(?=.*\\.)", "*")
请参见 regex演示
功能:
-
(?<=.)[^@](?=[^@]*?@)
-除@
([^@]
)之外的任何字符,其后跟任何单个字符((?<=.)
),后跟任意0个或多个除@
之外的字符,直至一个( (?=[^@]*?@)
) -
|
-或 -
(?:(?<=@.)|(?!^)\\G(?=[^@]*$))
-匹配字符串中以@
开头的位置,并匹配前一个成功匹配项((?!^)\\G
)末尾的任何字符((?<=@.)
)或(|
)char@
uo以外的任何0+字符到字符串((?=[^@]*$)
)的末尾 -
.
-任何单个字符 -
(?=.*\\.)
-后跟任意0+字符,直到字符串中的最后一个.
符号.
(?<=.)[^@](?=[^@]*?@)
-any char other than@
([^@]
) that is preceded by any single char ((?<=.)
) and is followed with any 0 or more chars other than@
up to a@
((?=[^@]*?@)
)|
- or(?:(?<=@.)|(?!^)\\G(?=[^@]*$))
- match a location in the string that is preceded with@
and any char ((?<=@.)
) or (|
) the end of the previous successful match ((?!^)\\G
) that is followed with any 0+ chars other than@
uo to the end of string ((?=[^@]*$)
).
- any single char(?=.*\\.)
- followed with any 0+ chars up to the last.
symbol in the string.
这篇关于电子邮件屏蔽的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
-