简体   繁体   中英

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"

Use the following solution:

x <- c("My best email gmail.com","email com", "email.com", "smail.com text here")
trimws(gsub("\\S+\\.com\\b", "", x))
## => [1] "My best email" "email com"     ""              "text here"

See the R demo.

The \\\\S+\\\\.com\\\\b pattern matches 1+ non-whitespace chars followed by a literal .com followed by the word boundary.

The trimws function will trim all the resulting strings (as, eg with "smail.com text here" , when a space will remain after smail.com removal).

Note that TRE regex engine does not support shorthand character classes inside bracket expressions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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