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)