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

Har*_*rpo 5 regex autohotkey

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

例如:

这是一些文字,它会继续这样,
直到有时电子邮件地址显示为asd@asd.com

还有一些文字在这里和这里.

结果我想要这个.

这是一些文字,它会继续这样,
直到有时电子邮件地址出现[email_removed]

还有一些文字在这里和这里.

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
}
Run Code Online (Sandbox Code Playgroud)

这个问题是我最终失去了所有换行符并且只获得了空格.如何在重新删除电子邮件地址之前重新构建字符串?

Cer*_*nce 4

看起来相当复杂,为什么不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
Run Code Online (Sandbox Code Playgroud)

根据您的需要,您可以随意使图案变得简单或复杂RegExReplace,但应该这样做。

  • 我无法重现该问题,您确定您准确复制了代码吗?结尾的“\S+”应该“仅”匹配非空白字符。当我测试字符串 ``string := "foo bar asd@asd.com`nbaz"`` 时,我得到 `foo bar [email_removed]\nbaz`,其中第二个中的 `\n` 是文字换行符特点。 (2认同)