I have a vector tissue which contains strings delimited by multiple characters. The constituent strings of the vector belong broadly to four classes:
Strings which only consist of term(s) (e.g.
ThymusThyroid) separated by,Strings which contain identifier(s) (e.g.
ECO:0000313|RefSeq:XP_014046664.1) ending with},followed by term(s) separated by,Strings which contain a term followed by identifier(s)
Strings which contain a term followed by an identifier and then term(s) separated by
,tissue <- c("Head kidney,Thymus,Thyroid,", "Red blood cell,", "ECO:0000313|RefSeq:XP_014046664.1},Muscle,", "ECO:0000313|RefSeq:XP_016683349.1},ECO:0000313|RefSeq:XP_016683354.1},Leaf,", "ECO:0000313|RefSeq:XP_014023833.1},Head kidney,Muscle,White muscle,", "Blood,ECO:0000313|RefSeq:XP_017326031.1},", "Spleen,ECO:0000313|RefSeq:XP_010844217.1},ECO:0000313|RefSeq:XP_010844218.1},", "Brain,ECO:0000313|RefSeq:XP_014030244.1},Head kidney,Muscle,Spleen,White muscle,")
For strings belonging to category 1, I could split the terms using a simple strsplit() function
unlist(strsplit("Head kidney,Thymus,Thyroid,", ","))
[1] "Head kidney" "Thymus" "Thyroid"
unlist(strsplit("Red blood cell,", ","))
[1] "Red blood cell"
For strings belonging to category 2, this is what I came up with and it works fine
unlist(strsplit(sub('.*\\},', "", "ECO:0000313|RefSeq:XP_014046664.1},Muscle,"), ","))
[1] "Muscle"
unlist(strsplit(sub('.*\\},', "", "ECO:0000313|RefSeq:XP_016683349.1},ECO:0000313|RefSeq:XP_016683354.1},Leaf,"), ","))
[1] "Leaf"
unlist(strsplit(sub('.*\\},', "", "ECO:0000313|RefSeq:XP_014023833.1},Head kidney,Muscle,White muscle,"), ","))
[1] "Head kidney" "Muscle" "White muscle"
For strings belonging to category 3, this worked for me
sub(',ECO:.*', "", "Blood,ECO:0000313|RefSeq:XP_017326031.1},")
[1] "Blood"
sub(',ECO:.*', "", "Spleen,ECO:0000313|RefSeq:XP_010844217.1},ECO:0000313|RefSeq:XP_010844218.1},")
[1] "Spleen"
For category 4, this is what I tried and it works fine
unlist(strsplit(sub(',ECO:.*},', ",", "Brain,ECO:0000313|RefSeq:XP_014030244.1},Head kidney,Muscle,Spleen,White muscle,"), ","))
[1] "Brain" "Head kidney" "Muscle" "Spleen" "White muscle"
I'm looking for a solution, a single regex if possible, which can handle all these conditions and can be used directly on the vector.