【问题标题】:Masking middle characters in email before '@'在“@”之前屏蔽电子邮件中的中间字符
【发布时间】:2021-02-08 06:38:36
【问题描述】:

我只想显示电子邮件的前两个和最后两个字符,如下所示:

Email -  123456789@gmail.com
result - 12*****89@gmail.com

我正在使用此正则表达式将匹配替换为 * - (?<=.{2}).*(?=.{2}@)

使用的代码 sn-p:

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

即使正则表达式匹配正确的东西,它总是用** 替换中间部分。但我希望每个角色都有一个*。是什么问题以及如何解决?

【问题讨论】:

  • 也许正则表达式不是最好的解决方案?⠀ 如果我必须维护这样的代码(或者特别像当前答案中更复杂的正则表达式),我会做很多比使用简单的split("@") &c 更令人头疼(可能还有代码破坏)——有点啰嗦但更容易理解。

标签: java regex kotlin regex-lookarounds


【解决方案1】:

在 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]
})

proof

结果:12*****89@gmail.com

模式说明

--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    [^@]{2}                  any character except: '@' (2 times)
--------------------------------------------------------------------------------
  )                        end of \1
--------------------------------------------------------------------------------
  (                        group and capture to \2:
--------------------------------------------------------------------------------
    [^@]+                    any character except: '@' (1 or more
                             times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
  )                        end of \2
--------------------------------------------------------------------------------
  (                        group and capture to \3:
--------------------------------------------------------------------------------
    [^@]{2}                  any character except: '@' (2 times)
--------------------------------------------------------------------------------
    @                        '@'
--------------------------------------------------------------------------------
  )                        end of \3

【讨论】:

    【解决方案2】:

    你可以使用

    .replaceAll("(\\G(?!^)|^[^@]{2})[^@](?=[^@]{2,}@)", "$1*")
    

    请参阅regex demo

    • (\G(?!^)|^[^@]{2}) - 第 1 组 ($1):上一场比赛的结尾或字符串开头的两个非@ 字符
    • [^@] - 任何非@ 字符
    • (?=[^@]{2,}@) - 后跟 2 个或更多非 @ 字符,最多为 @ 字符。

    Java demo

    String email = "123456789@gamil.com";
    System.out.println(email.replaceAll("(\\G(?!^)|^[^@]{2})[^@](?=[^@]{2,}@)", "$1*"));
    // => 12*****89@gamil.com
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-09-29
      • 1970-01-01
      • 2015-05-10
      • 1970-01-01
      • 2020-10-11
      • 1970-01-01
      • 2021-02-12
      相关资源
      最近更新 更多