Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How remove email-address from string?

So, I have a string and I want to remove the e-mail adress from it if there is one.

As example:

This is some text and it continues like this
until sometimes an email adress shows up [email protected]

also some more text here and here.

I want this as a result.

This is some text and it continues like this
until sometimes an email adress shows up [email_removed]

also some more text here and here.

cleanFromEmail(string)
{
    newWordString = 
    space := a_space
    Needle = @
    wordArray := StrSplit(string, [" ", "`n"])
    Loop % wordArray.MaxIndex()
    {

        thisWord := wordArray[A_Index]


        IfInString, thisWord, %Needle%
        {
            newWordString = %newWordString%%space%(email_removed)%space%
        }
        else
        {
            newWordString = %newWordString%%space%%thisWord%%space%
            ;msgbox asd
        }
    }

    return newWordString
}

The problem with this is that I end up loosing all the line-breaks and only get spaces. How can I rebuild the string to look just like it did before removing the email-adress?

like image 253
Harpo Avatar asked Feb 20 '19 09:02

Harpo


1 Answers

That looks rather complicated, why not use RegExReplace instead?

string =
(
This is some text and it continues like this
until sometimes an email adress shows up [email protected]

also some more text here and here.
)

newWordString := RegExReplace(string, "\S+@\S+(?:\.\S+)+", "[email_removed]")

MsgBox, % newWordString

Feel free to make the pattern as simple or as complicated as you want, depending on your needs, but RegExReplace should do it.

like image 59
CertainPerformance Avatar answered Oct 01 '22 18:10

CertainPerformance