How to find email domain using stringr_match

If i have an example email like fakeemail@gmail.com how do I extract just the "com" using stringr_match?

The function is str_match() in the package stringr. Have you checked the documentation with ?str_match ? Have you tried something that didn't work?

1 Like

Thank you, the issue has been solved aready!

In the code below, I generate 10 fake emails and perform the operation that you want. As pointed out by @AlexisW, the function is str_match() in the stringr package. However, I use a different function: str_extract(), which is also in the stringr package.

library(stringr)
library(stringi)

# Generate fake emails
fake_emails <- paste0(stri_rand_strings(n = 10, length = 5), "@domain.", sample(c("com", "net", "org"), size = 10, replace = TRUE))
fake_emails

[1] "GNZuC@domain.net" "twed3@domain.com" "CAgNl@domain.com" "UizNm@domain.org" "vDe7G@domain.net" "N0NrL@domain.net" "TbUBp@domain.org" "fn6iP@domain.org"
 [9] "oemYW@domain.net" "m1Tjg@domain.net"

# Extract string
str_extract(fake_emails, "\\w{3}$")

[1] "net" "com" "com" "org" "net" "net" "org" "org" "net" "net"
1 Like

If there is only one . you can use

> stringr::str_match("fakeemail@gmail.com","(?<=\\.).+$")

(?<=\\.).+$
means "look backwards for a . then match one or more of any character until the end of the string".
If there is more than one . you can use

stringr::str_match("fakeemail.oops@gmail.com","(?<=\\.)[^\\.]+$")

(?<=\\.)[^\\.]+$
means "look backwards for a . then match one or more of any character that is not . until the end of the string".

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.