Home > Enterprise >  extract until next "_" if contains
extract until next "_" if contains

Time:07-15

Is there a way to extract part of string, when there is a match (everything up to the next underscore) "_"?

From: mycampaign_s22uhd4k_otherinfo I need: s22uhd4k.
From: my_campaign_otherinfo_s22jumpto_otherinfo , I would need: s22jumpto

data:

df <- structure(list(a = c("mycampaign_s22uhd4k_otherinfo", "my_campaign_otherinfo_s22jumpto_otherinfo"
), b = c(1, 2)), class = "data.frame", row.names = c(NA, -2L))

CodePudding user response:

Do either of these options work with your real data?

df <- structure(list(a = c("mycampaign_s22uhd4k_otherinfo", "my_campaign_otherinfo_s22jumpto_otherinfo"
), b = c(1, 2)), class = "data.frame", row.names = c(NA, -2L))

gsub(df$a, pattern = ".*_(.*)_.*", replacement = "\\1", perl = TRUE)
#> [1] "s22uhd4k"  "s22jumpto"

gsub(df$a, pattern = ".*(s22.*)_.*", replacement = "\\1", perl = TRUE)
#> [1] "s22uhd4k"  "s22jumpto"

Created on 2022-07-15 by the reprex package (v2.0.1)

  • Related