正则表达式 – 如何从字符串中删除电子邮件地址?

前端之家收集整理的这篇文章主要介绍了正则表达式 – 如何从字符串中删除电子邮件地址?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
所以,我有一个字符串,我想删除它的电子邮件地址,如果有的话.

例如:

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应该这样做.

猜你在找的正则表达式相关文章