0

I have the following code that will extract an email address from a text string, which will get replaced by text inside a database field.

The string will contain a single email address. I have already split the email address as follows:

  1. test@test.co.za - Full email address
  2. test - email user
  3. test.co.za - Domain

I have included code that can replace all characters as *************** (Full Email) or the individual parts.

It would be easy to combine the text again when I have done what I needed.

The result I am trying to get:

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

Was thinking about hiding everything in the first part, except first 2 characters and in the last part, everything except last 2 characters.

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)

Any help would be greatly appreciated.

Deedz
  • 127
  • 1
  • 10

2 Answers2

4

Perhaps try this instead:

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)

It gives: te**@********za

Enigmativity
  • 113,464
  • 11
  • 89
  • 172
0

Set up a pattern with a nested substring in the current email user substring. Apply the **** filter to only the nested part of the substring.

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

See Here for more info on nested Substrings in Regex

JustinCoplin
  • 169
  • 3
  • 16