英文:
Masking middle characters in email before '@'
问题
我想仅显示电子邮件的前两个和最后两个字符,如下所示:
电子邮件 - 123456789@gmail.com
结果 - 12*****89@gmail.com
我正在使用以下正则表达式将匹配项替换为* - (?<=.{2}).*(?=.{2}@)
。
使用的代码片段:
String email = "123456789@gamil.com";
System.out.println(email.replaceAll("(?<=.{3}).*(?=.{3}@)", "*"));
// 打印 - 12**89@gamil.com
// 仅在中间添加2个**
// 每个替换字符都需要一个*
尽管正则表达式匹配了正确的内容,但它总是用**
替换中间部分。但我想要每个字符一个*
。问题在哪里,如何解决?
英文:
I want to show only the first two and the last two characters of an email like the following:
Email - 123456789@gmail.com
result - 12*****89@gmail.com
I am using this regex to replace the matches with * - (?<=.{2}).*(?=.{2}@)
.
Code snippet used:
String email = "123456789@gamil.com";
System.out.println(email.replaceAll("(?<=.{3}).*(?=.{3}@)", "*"));
// prints - 12**89@gamil.com
// Adds only 2 ** in the middle
// Required * for each replaced character like - 12*****89@gmail.com
Even though the regex matches the correct thing, It always replaces the middle part with **
. But I want a *
for each character. What is the problem and how to fix it?
答案1
得分: 1
你可以使用
.replaceAll("(\\G(?!^)|^[^@]{2})[^@](?=[^@]{2,}@)", "$1*")
查看正则表达式演示。
(\G(?!^)|^[^@]{2})
- 第一组 ($1
):前一个匹配的结束或者字符串开头的两个非@
字符[^@]
- 任意非@
字符(?=[^@]{2,}@)
- 接着是2个或更多非@
字符,直到一个@
字符。
查看Java演示:
String email = "123456789@gamil.com";
System.out.println(email.replaceAll("(\\G(?!^)|^[^@]{2})[^@](?=[^@]{2,}@)", "$1*"));
// => 12*****89@gamil.com
英文:
You can use
.replaceAll("(\\G(?!^)|^[^@]{2})[^@](?=[^@]{2,}@)", "$1*")
See the regex demo.
(\G(?!^)|^[^@]{2})
- Group 1 ($1
): end of the previous match or two non-@
chars at the start of the string[^@]
- any non-@
char(?=[^@]{2,}@)
- followed with 2 or more non-@
chars up to a@
char.
See the Java demo:
String email = "123456789@gamil.com";
System.out.println(email.replaceAll("(\\G(?!^)|^[^@]{2})[^@](?=[^@]{2,}@)", "$1*"));
// => 12*****89@gamil.com
答案2
得分: 1
val s = "123456789@gmail.com";
val p = """^([^@]{2})([^@]+)([^@]{2}@)""".toRegex();
println(s.replace(p) {
it.groupValues[1] + "*".repeat(it.groupValues[2].length) + it.groupValues[3]
})
查看证据。
结果:12*****89@gmail.com
。
模式解释
--------------------------------------------------------------------------------
^ 字符串的开头
--------------------------------------------------------------------------------
( 捕获组 :
--------------------------------------------------------------------------------
[^@]{2} 除了 '@' 之外的任意字符(2次)
--------------------------------------------------------------------------------
) 的结束
--------------------------------------------------------------------------------
( 捕获组 :
--------------------------------------------------------------------------------
[^@]+ 除了 '@' 之外的任意字符(1次或更多
次(匹配最大数量)
--------------------------------------------------------------------------------
) 的结束
--------------------------------------------------------------------------------
( 捕获组 :
--------------------------------------------------------------------------------
[^@]{2} 除了 '@' 之外的任意字符(2次)
--------------------------------------------------------------------------------
@ '@'
--------------------------------------------------------------------------------
) 的结束
<details>
<summary>英文:</summary>
In Kotlin, use
```kotlin
val s = "123456789@gmail.com"
val p = """^([^@]{2})([^@]+)([^@]{2}@)""".toRegex()
println(s.replace(p) {
it.groupValues[1] + "*".repeat(it.groupValues[2].length) + it.groupValues[3]
})
See proof.
Result: 12*****89@gmail.com
.
Pattern explanation
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
( group and capture to :
--------------------------------------------------------------------------------
[^@]{2} any character except: '@' (2 times)
--------------------------------------------------------------------------------
) end of
--------------------------------------------------------------------------------
( group and capture to :
--------------------------------------------------------------------------------
[^@]+ any character except: '@' (1 or more
times (matching the most amount
possible))
--------------------------------------------------------------------------------
) end of
--------------------------------------------------------------------------------
( group and capture to :
--------------------------------------------------------------------------------
[^@]{2} any character except: '@' (2 times)
--------------------------------------------------------------------------------
@ '@'
--------------------------------------------------------------------------------
) end of
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论