【问题标题】:Visual Basic Replace certain characters in email address for privacyVisual Basic 替换电子邮件地址中的某些字符以保护隐私
【发布时间】:2018-04-13 14:52:43
【问题描述】:

我有以下代码将从文本字符串中提取电子邮件地址,该地址将被数据库字段中的文本替换。

字符串将包含一个电子邮件地址。我已经将邮箱地址拆分如下:

  1. test@test.co.za - 完整的电子邮件地址
  2. 测试 - 电子邮件用户
  3. test.co.za - 域

我已包含可以将所有字符替换为 ***************(完整电子邮件)或单个部分的代码。

当我完成我需要的工作后,再次组合文本会很容易。

我想要得到的结果:

te**@********za

正在考虑隐藏第一部分中的所有内容,除了前 2 个字符,在最后一部分中,除了最后 2 个字符之外的所有内容。

Dim newText As string = ""

Dim senText As String = "This is a long string that we will try test@test.co.za to extract the email address from, it might exist anywhere in this string."
Dim emailRegEx As New Regex("(\S+)@([^\.\s]+)(?:\.([^\.\s]+))+")
Dim m As Match = emailRegEx.Match(senText)
If m.Success Then
    newText = m.ToString
End If

Dim mystr As String = newText
Dim cut_at As String = "@"
Dim x As Integer = InStr(mystr, cut_at)

Dim string_before As String = mystr.Substring(0, x - 1)
Dim string_after As String = mystr.Substring(x + cut_at.Length - 1)

Dim myString As String = New String("*"c, newText.Length)

Dim PrivateMail As String = ""

response.write(newText)
response.write(myString)
response.write(string_before)
response.write(string_after)

任何帮助将不胜感激。

【问题讨论】:

    标签: regex vb.net vba


    【解决方案1】:

    在当前电子邮件用户子字符串中设置具有嵌套子字符串的模式。将 **** 过滤器仅应用于子字符串的嵌套部分。

    Dim emailRegEx As New Regex("(\S{1,2}(\S*))@([^\.\s]+)(?:\.([^\.\s]+))+")
    

    请参阅Here 了解更多关于正则表达式中嵌套子字符串的信息

    【讨论】:

      【解决方案2】:

      也许可以试试这个:

      Dim email = "test@test.co.za"
      
      Dim obsfucated = _
          String.Join("@", email.Split("@"c).Select(Function(x, n) _
              If(n = 0, _
                  x.Substring(0, System.Math.Min(2, x.Length)).PadRight(x.Length, "*"c), _
                  x.Substring(x.Length - 2, 2).PadLeft(x.Length, "*"c))))
      
      Console.WriteLine(obsfucated)
      

      它给出:te**@********za

      【讨论】:

      • 非常感谢@enigmativity,您是传奇先生!
      猜你喜欢
      • 2022-01-17
      • 1970-01-01
      • 2018-10-21
      • 1970-01-01
      • 2021-06-17
      • 1970-01-01
      • 2017-05-10
      • 2019-12-26
      • 2014-02-20
      相关资源
      最近更新 更多