问题
I am struggling to remove the substring before the underscore in my string. I want to use * (wildcard) as the bit before the underscore can vary:
a <- c(\"foo_5\", \"bar_7\")
a <- gsub(\"*_\", \"\", a, perl = TRUE)
The result should look like:
> a
[1] 5 7
I also tried stuff like \"^*\" or \"?\" but did not really work.
回答1:
The following code works on your example :
gsub(".*_", "", a)
回答2:
Alternatively, you can also try:
gsub("\\S+_", "", a)
回答3:
as.numeric(gsub(pattern=".*_", replacement = '', a)
[1] 5 7
来源:https://stackoverflow.com/questions/11776287/remove-pattern-from-string-with-gsub