mRiddle
mRiddle

Reputation: 214

Characters before/after a symbol

I have the following string in R: "xxx, yyy. zzz" I want to get the yyy part only, which are in between "," and "."

I don't want to use regex.

I searched half a day, found many string functions in R but none which deal with "cut before/after a character" function.

Is there such?

Upvotes: 2

Views: 4120

Answers (2)

r.user.05apr
r.user.05apr

Reputation: 5456

It could be that this suffices:

unlist(strsplit("xxx, yyy. zzz","[,.]"))[2] # get yyy with space, or:
gsub(" ","",unlist(strsplit("xxx, yyy. zzz","[,.]")))[2] # remove space

Upvotes: 1

akrun
akrun

Reputation: 887431

We can use gsub to match zero or more characters that are not a , ([^,]*) from the start (^) of the string followed by a , followed by zero or more spaces (\\s*) or (!) a dot (\\. - it is a metacharacter meaning any character so it is escaped) followed by other characters (.*) until the end of the string ($) and replace it with blank ("")

 gsub("^[^,]*,\\s*|\\..*$", "", str1)
 #[1] "yyy"

If we don't need regex then strsplit the string by , followed by zero or more spaces or with a . and select the second entry after converting the list output to vector ([[1]])

strsplit(str1, ",\\s*|\\.")[[1]][2]
#[1] "yyy"

data

str1 <-  "xxx, yyy. zzz" 

Upvotes: 3

Related Questions