简体   繁体   English

使用gsub替换电子邮件

[英]Replace emails using gsub

How can I replace an email regex pattern to missing. 如何将电子邮件regex模式替换为丢失。 I have tried 我努力了

blogs.smpl <- "mami@yahoo.com : subject:Lorem Ipsum body:   is simply dummy text of the printing and typesetting industry.Lorem@Ipsum.com has been the industry's standard dummy text ever since the 1500s"
blogs.smpl1 <- gsub("^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9.-]+$","",blogs.smpl )
blogs.smpl1

But doesn't replace the email addresses. 但不要替换电子邮件地址。 I have also tried using this pattern email2 <- "^[[:alnum:].-]+@[[:alnum:].-]+$" 我也尝试过使用这种模式email2 <- "^[[:alnum:].-]+@[[:alnum:].-]+$"

The anchors around your entire pattern are the problem. 在你的整个模式都是问题。 The ^ anchor asserts that the regex engine's current position in the string is the beginning of the string and $ asserts the position at the end of the string. ^锚断言正则表达式引擎在字符串中的当前位置是字符串的开头,而$断言在字符串末尾的位置。 These addresses are at neither position in your string. 这些地址不在您的字符串中。 So, by implementing both anchors, you're telling the engine your entire string should match this pattern. 因此,通过实现两个锚点,您就是在告诉引擎您的整个字符串应与此模式匹配。

You could simply remove both of the anchors and it would replace the addresses in your data. 您只需删除两个锚点,它就会替换数据中的地址。 Alternatively, if you know these addresses are always bounded by whitespace, this would suffice. 或者,如果您知道这些地址始终由空格限制,则就足够了。

gsub('\\S+@\\S+', '', blogs.smpl)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM