如何从字符串中删除电子邮件地址?

How remove email-address from string?

所以,我有一个字符串,如果有的话,我想从中删除电子邮件地址。

例如:

This is some text and it continues like this
until sometimes an email adress shows up asd@asd.com

also some more text here and here.

我想要这个结果。

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
}

这个问题是我最终失去了所有换行符而只得到空格。我怎样才能重建字符串,使其看起来像删除电子邮件地址之前一样?

这看起来很复杂,为什么不使用 RegExReplace 呢?

string =
(
This is some text and it continues like this
until sometimes an email adress shows up asd@asd.com

also some more text here and here.
)

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

MsgBox, % newWordString

根据您的需要,您可以随意使模式尽可能简单或 complicated,但 RegExReplace 应该这样做。

如果出于某种原因 RegExReplace 并不总是适合您,您可以试试这个:

text =
(
This is some text and it continues like this
until sometimes an email adress shows up asd@asd.com.

also some more text here and here.
)

MsgBox, % cleanFromEmail(text)

cleanFromEmail(string){
    lineArray := StrSplit(string, "`n")
    Loop % lineArray.MaxIndex()
    {
        newLine := ""
        newWord := ""
        thisLine := lineArray[A_Index]
        If InStr(thisLine, "@")
        {
            wordArray := StrSplit(thisLine, " ")
            Loop % wordArray.MaxIndex()
            {
                thisWord := wordArray[A_Index]
                {
                    If InStr(thisWord, "@")
                    {
                        end := SubStr(thisWord, 0)
                        If end in ,,,.,;,?,!
                            newWord := "[email_removed]" end ""
                        else
                            newWord := "[email_removed]"
                    }
                    else
                        newWord := thisWord
                }
                newLine .= newWord . " " ; concatenate the outputs by adding a space to each one
            }
            newLine :=  trim(newLine) ; remove the last space from this variable
        }
        else
            newLine := thisLine
        newString .= newLine . "`n"
    }
    newString := trim(newString)
    return newString
}