Remove pattern from string with gsub

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.

Answers


The following code works on your example :

gsub(".*_", "", a)

Alternatively, you can also try:

gsub("\\S+_", "", a)

as.numeric(gsub(pattern=".*_", replacement = '', a)
[1] 5 7

Need Your Help

Laravel 5 get view name

php templates view laravel-5

I'm struggling to get the view name in L5. Just as in WP, I'd like to add a specific page name (view name) for styling, like so: