Extract string till delimiter

Hi all,

I have a string that has values such as
[1] "WEEK -2-0 (VISIT 02-03)" "WEEK 1-4 (VISIT 04-07)" "WEEK 22-26 (VISIT 20-22)" "WEEK 9-12 (VISIT 12-15)"

I would like to extract substring out of each string such as it displays as
[1] "WEEK -2-0 " "WEEK 1-4 " "WEEK 22-26" "WEEK 9-12"

so, the intention is to get all the strings till the delimiter '(' comes.

Maybe this could help if I understand well.

my_strings <- c("WEEK -2-0 (VISIT 02-03)", "WEEK 1-4 (VISIT 04-07)", "WEEK 22-26 (VISIT 20-22)", "WEEK 9-12 (VISIT 12-15)")

#  regular expressions
my_substrings <- gsub("\\s*\\(.*\\)", "", my_strings)

my_substrings
# "WEEK -2-0"  "WEEK 1-4"   "WEEK 22-26" "WEEK 9-12" 

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.