I want to extract a part of the string that comes before a certain word. E.g. I want to get everything before \", useless\".
a <- \"Experiment A, useless (03/
We can use sub to match the , followed by zero or more spaces (\\s*) followed by 'useless' and other characters that follow (.*) and replace it with blank ("")
sub
,
\\s*
.*
""
sub(",\\s*useless\\b.*", "", a) #[1] "Experiment A"