Similar questions have been asked here and here. However, I can't seem to get them to work for me.
If I have a character vector like:
myString <- c("5", "10", "100\abc\nx1\n1")
I want to remove everything after (and including) the first backslash. For example, my expected result would be:
>myString
"5" "10" "100"
I have tried using sub
, gsub
, and strsplit
but I just can't seem to get it to work. Things I've tried:
gsub("\\\\*", "", myString)
sub("\\\\.*", "", myString)
gsub('\\"', "", myString, fixed = TRUE)
gsub("\\.*","", myString)
But I'm not great with regex stuff so I'm almost definitely not using these functions correctly! Any advice as to how I'd fix this?