更新时间:2022-11-14 23:35:32
foo@bar.com
⇒f**@b**.com
(当前问题)-s.replaceAll("(?<=.)[^@](?=[^@]*?@)|(?:(?<=@.)|(?!^)\\G(?=[^@]*$)).(?=.*\\.)", "*")
(请参见正则表达式演示)
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.