extract until next "_" if contains

Viewed 43

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))
1 Answers

Thanks Omar, based on your update/comment, this regex will solve your problem:

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

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

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

Explanation:

.*(s22[^_]+(?=_)).*

.* match all characters up until the first capture group

(s22 the first capture group starts with "s22"

[^_]+ after "s22", match any character except "_"

(?=_) until the next "_" (positive look ahead)

) close the first capture group

.* match all remaining characters

Then, the replacement = "\\1" means to just print the captured text (the part you want)

Related