问题
Look at following vector:
x <- c("MED - This means medic - somecode123", "HIV" - This means HIV -somecode456")
Now I want the vector: containing the values
This means medic - somecode123`
This means HIV - somecode1456
I seem not able to solve this using gsub
...
回答1:
We can use sub
. Match the pattern of one or more non-white space (\\S+
) followed by one or more white space (\\s+
) followed by -
and white space (\\s+
) and replace it with ""
.
sub('\\S+\\s+-\\s+', "", x)
#[1] "This means medic - somecode123" "This means HIV -somecode456"
来源:https://stackoverflow.com/questions/36158204/remove-all-text-before-first-occurence-of-specific-characeter-in-r