36

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.

Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519
user969113
  • 2,349
  • 10
  • 44
  • 51

4 Answers4

59

The following code works on your example :

gsub(".*_", "", a)
Pop
  • 12,135
  • 5
  • 55
  • 68
  • great thanks. What does the '.' stands for? Why does the star "*" (wildcard) does not work by its own? I don't understand? – user969113 Aug 02 '12 at 11:20
  • 4
    '.' stands for any character, so that ".*" stands for any number of any character. You can find more explanations by typing `?regex`. – Pop Aug 02 '12 at 11:23
  • 1
    Ah I see! I thought * includes the meaning of the '.' already! Well, now I know better. thanks again! – user969113 Aug 02 '12 at 13:51
  • 1
    For clarification, you are confusing the regular expression (regex) syntax with glob syntax; both use the * star character as a wild card, but it means slightly different things between the two. – user5359531 Apr 29 '16 at 02:44
8

Alternatively, you can also try:

gsub("\\S+_", "", a)
Madhu Sareen
  • 549
  • 1
  • 8
  • 20
2

Just to point out that there is an approach using functions from the tidyverse, which I find more readable than gsub:

a %>% stringr::str_remove(pattern = ".*_")
elcortegano
  • 2,444
  • 11
  • 40
  • 58
  • a is a vector not a dataframe. This would result in the following error: 'Warning message: In stri_replace_first_regex(string, pattern, fix_replacement(replacement), : argument is not an atomic vector; coercing" – Colin McGovern Mar 09 '22 at 20:00
-2
as.numeric(gsub(pattern=".*_", replacement = '', a)
[1] 5 7
Nathan Tuggy
  • 2,237
  • 27
  • 30
  • 38
Praveen
  • 19