Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extracting text after last period in string [duplicate]

Tags:

regex

r

I realize this question probably seems painfully simple to most regular expression masters, but reviewing similar questions has not yielded a solution.

I have a vector of e-mail addresses called email and would like to extract the text after the final period in each one. For the sake of example,

email<-c("[email protected]", "[email protected]", "[email protected]")

I have tried:

grep("[\.][a-zA-Z]*?$", email, value=T)

This gets me the error message:

Error: '.' is an unrecognised escape in character string starting ""."`

Removing the escape character on the other hand

grep("[.][a-zA-Z]*?$", email, value=T)

returns the entire e-mail address as does:

grep("\\.[a-zA-Z]*$", email, perl=T, value=T)

I'd really appreciate help at this point.

like image 381
user2230555 Avatar asked Aug 02 '15 16:08

user2230555


People also ask

How do I extract text after a period in Excel?

Extract text before or after space with formula in Excel Select a blank cell, and type this formula =LEFT(A1,(FIND(" ",A1,1)-1)) (A1 is the first cell of the list you want to extract text) , and press Enter button.


1 Answers

If you need to extract the string after the last period (.), try with sub

sub('.*\\.', '', email)
#[1] "com" "com"

data

email <- c('[email protected]', 'xxx$xxxx.com')
like image 113
akrun Avatar answered Oct 06 '22 06:10

akrun