nan
nan

Reputation: 401

how to separate names using regular expression?

I have a name vector like the following:

 vname<-c("T.Lovullo (73-58)","K.Gibson (63-96) and A.Trammell (1-2)","T.La Russa (81-81)","C.Dressen (16-10), B.Swift (32-25) and F.Skaff (40-39)")

Watch out for T.La Russa who has a space in his name

I want to use str_match to separate the name. The difficulty here is that some characters contain two names while the other contain only one like the example I gave. I have write my code but it does not work:

str_match_all(ss,"(D[.]D+.+)s(\\(d+-d+\\))(s(and)s(D[.]D+.+)s(\\(d+-d+\\)))?")

Upvotes: 0

Views: 41

Answers (1)

akrun
akrun

Reputation: 887213

Perhaps this helps

res <- unlist(strsplit(vname, "(?<=\\))(\\sand\\b\\s)*", perl = TRUE))
res
#[1] "T.Lovullo (73-58)"  "K.Gibson (63-96)"   "A.Trammell (1-2)"   "T.La Russa (81-81)"

To get the names only (if that is what the expected)

sub("\\s*\\(.*", "", res)
#[1] "T.Lovullo"  "K.Gibson"   "A.Trammell" "T.La Russa"

Upvotes: 2

Related Questions