user1569897 user1569897 - 3 years ago 56
R Question

How to delete only (anyword).com in Regex?

I'd like to match the following

My best email gmail.com
email com
email.com


to become

My best email
email com
*nothing*


Specifically, I'm using Regex for R, so I know there are different rules for escaping certain characters. I'm very new to Regex, but so far I have

\ .*(com)


which makes the same input

My


But this code does not work for instances where there are no spaces like the third example, and removes everything past the first space of a line if the line has a ".com"

Answer Source

You need to escape the ., not the space, to make it a literal dot instead of "any character". You also don't need the star and you don't want the capture group to be around the com but rather around the whole email:

([^\s]*\.com)

[regex101 link]

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download