[英]Regular expression for email masking
I am trying to write a regular expression to mask an email address. 我正在尝试编写一个正则表达式来掩盖电子邮件地址。 Example below.
下面的例子。
input: john.doe@example.en.com
输入:john.doe@example.en.com
output: j*******@e*********.com
输出:j*******@e*********.com
I have tried the following but I just can't seem to get it working correctly. 我尝试了以下方法,但似乎无法正常工作。
regex:
(?<=.).(?=[^@]\\*?@)
正则表达式:
(?<=.).(?=[^@]\\*?@)
output:j*******@example.en.com
输出:j ******* @ example.en.com
regex:
(?<=.).(?=[^@]\\*?)(?=[^\\.]\\*?\\.)
regex:
(?<=.).(?=[^@]\\*?)(?=[^\\.]\\*?\\.)
output:j******************.com
输出:j ******************。com
foo@bar.com
⇒ f**@b**.com
(current question) - s.replaceAll("(?<=.)[^@](?=[^@]*?@)|(?:(?<=@.)|(?!^)\\\\G(?=[^@]*$)).(?=.*\\\\.)", "*")
(see the regex demo ) foo@bar.com
f**@b**.com
(当前问题) s.replaceAll("(?<=.)[^@](?=[^@]*?@)|(?:(?<=@.)|(?!^)\\\\G(?=[^@]*$)).(?=.*\\\\.)", "*")
(请参阅regex演示 )
foo@bar.com
⇒ f**@b*r.com
- s.replaceAll("(?<=.)[^@](?=[^@]*?@)|(?:(?<=@.)|(?!^)\\\\G(?=[^@]*$)).(?=.*[^@]\\\\.)", "*")
(see the regex demo ) foo@bar.com
f**@b*r.com
s.replaceAll("(?<=.)[^@](?=[^@]*?@)|(?:(?<=@.)|(?!^)\\\\G(?=[^@]*$)).(?=.*[^@]\\\\.)", "*")
f**@b*r.com
- s.replaceAll("(?<=.)[^@](?=[^@]*?@)|(?:(?<=@.)|(?!^)\\\\G(?=[^@]*$)).(?=.*[^@]\\\\.)", "*")
(请参阅regex演示 )
foo@bar.com
⇒ f*o@b*r.com
- s.replaceAll("(?<=.)[^@](?=[^@]*?[^@]@)|(?:(?<=@.)|(?!^)\\\\G(?=[^@]*$)).(?=.*[^@]\\\\.)", "*")
(see the regex demo ) foo@bar.com
⇒ f*o@b*r.com
- s.replaceAll("(?<=.)[^@](?=[^@]*?[^@]@)|(?:(?<=@.)|(?!^)\\\\G(?=[^@]*$)).(?=.*[^@]\\\\.)", "*")
见的正则表达式演示 )
foo@bar.com
⇒ f**@b*****m
- s.replaceAll("(?<=.)[^@](?=[^@]*?@)|(?:(?<=@.)|(?!^)\\\\G(?=[^@]*$)).(?!$)", "*")
(see the regex demo ) foo@bar.com
f**@b*****m
- s.replaceAll("(?<=.)[^@](?=[^@]*?@)|(?:(?<=@.)|(?!^)\\\\G(?=[^@]*$)).(?!$)", "*")
(请参阅regex演示 )
foo@bar.com
⇒ f*o@b*****m
- s.replaceAll("(?<=.)[^@](?=[^@]*[^@]@)|(?:(?<=@.)|(?!^)\\\\G(?=[^@]*$)).(?!$)", "*")
(see the regex demo ) 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(?=[^@]*$)).(?=.*\\.)", "*")
See the regex demo 见正则表达式演示
What it does : 它的作用是 :
(?<=.)[^@](?=[^@]*?@)
-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 @
( (?=[^@]*?@)
) (?<=.)[^@](?=[^@]*?@)
-除@
( [^@]
)以外的任何字符,其后跟任何单个字符( (?<=.)
),并且后跟@
以外的0或更多字符,直至@
( (?=[^@]*?@)
) |
- 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 ( (?=[^@]*$)
) (?:(?<=@.)|(?!^)\\\\G(?=[^@]*$))
-匹配字符串中以@
和任何字符开头的位置( (?<=@.)
)或( |
)上一个成功匹配项( (?!^)\\\\G
)的末尾,其后跟@
uo以外的任何0+字符到字符串的末尾( (?=[^@]*$)
) .
- any single char (?=.*\\\\.)
- followed with any 0+ chars up to the last .
(?=.*\\\\.)
-后面跟有0个以上的字符.
symbol in the string. How about this one if you do not need the masks having the same number of characters of the original strings (which is more anonymous): 如果您不需要具有与原始字符串相同数目的字符(更匿名)的掩码,该怎么办:
(?<=^.)[^@]*|(?<=@.).*(?=\.[^.]+$)
For example, if you replace the matches with ***
, the result would be: 例如,如果将匹配项替换为
***
,则结果将是:
j***@e***.com
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.