在电子邮件地址的“@”符号之前屏蔽中间字符。

huangapple go评论78阅读模式
英文:

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 * - (?&lt;=.{2}).*(?=.{2}@).

Code snippet used:

String email = &quot;123456789@gamil.com&quot;;
System.out.println(email.replaceAll(&quot;(?&lt;=.{3}).*(?=.{3}@)&quot;, &quot;*&quot;));

// 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(&quot;(\\G(?!^)|^[^@]{2})[^@](?=[^@]{2,}@)&quot;, &quot;$1*&quot;)

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 = &quot;123456789@gamil.com&quot;;
System.out.println(email.replaceAll(&quot;(\\G(?!^)|^[^@]{2})[^@](?=[^@]{2,}@)&quot;, &quot;$1*&quot;));
// =&gt; 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}                  除了 &#39;@&#39; 之外的任意字符(2次)
--------------------------------------------------------------------------------
  )                         的结束
--------------------------------------------------------------------------------
  (                        捕获组 :
--------------------------------------------------------------------------------
    [^@]+                    除了 &#39;@&#39; 之外的任意字符(1次或更多
                             次(匹配最大数量)
--------------------------------------------------------------------------------
  )                         的结束
--------------------------------------------------------------------------------
  (                        捕获组 :
--------------------------------------------------------------------------------
    [^@]{2}                  除了 &#39;@&#39; 之外的任意字符(2次)
--------------------------------------------------------------------------------
    @                        &#39;@&#39;
--------------------------------------------------------------------------------
  )                         的结束

<details>
<summary>英文:</summary>

In Kotlin, use
```kotlin
val s = &quot;123456789@gmail.com&quot;
val p = &quot;&quot;&quot;^([^@]{2})([^@]+)([^@]{2}@)&quot;&quot;&quot;.toRegex()
println(s.replace(p) { 
    it.groupValues[1] + &quot;*&quot;.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: &#39;@&#39; (2 times)
--------------------------------------------------------------------------------
  )                        end of 
--------------------------------------------------------------------------------
  (                        group and capture to :
--------------------------------------------------------------------------------
    [^@]+                    any character except: &#39;@&#39; (1 or more
                             times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
  )                        end of 
--------------------------------------------------------------------------------
  (                        group and capture to :
--------------------------------------------------------------------------------
    [^@]{2}                  any character except: &#39;@&#39; (2 times)
--------------------------------------------------------------------------------
    @                        &#39;@&#39;
--------------------------------------------------------------------------------
  )                        end of 

huangapple
  • 本文由 发表于 2020年10月26日 07:17:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/64529786.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定