I have my string as:
cc <- c("Bacter;httyh;ttyyyt", "Bacteria;hhhdh;hhgt;hhhg", "Bacter;hhhhdj;gg;dd", "Bactr;hhhg;ggj", "Bctg;hhgg;hhj")
I would like to replace any text matching Bact before first ;  and replace it with Bctr.
I tried:
gsub("[Bact*]+;", "Bctr", cc)
So, the result I would like is
Bctr;httyh;ttyyyt, Bctr;hhhdh;hhgt;hhhg, Bctr;hhhhdj;gg;dd, Bctr;hhhg;ggj, Bctg;hhgg;hhj
Any suggestion what I am missing here?
We can use sub and replace from "Bact" till the first semi-colon with "Bctr";
sub("Bact.*?;", "Bctr;", cc)
#[1] "Bctr;httyh;ttyyyt" "Bctr;hhhdh;hhgt;hhhg" "Bctr;hhhhdj;gg;dd"  "Bctr;hhhg;ggj"
*? is used for lazy matching making it to match as few characters as possible. So here it stops after matching with first semi-colon. 
The difference would be clear if we remove ? from it. 
sub("Bact.*;", "Bctr;", cc)
#[1] "Bctr;ttyyyt" "Bctr;hhhg"   "Bctr;dd"     "Bctr;ggj"
Now it matches till the last semi-colon in cc.
ifelse(grepl("Bact", cc),
       paste0("Bctr", substring(cc,
                                attr(regexpr("Bact.*?;", cc), "match.length"),
                                nchar(cc))),
       cc)
#[1] "Bctr;httyh;ttyyyt"    "Bctr;hhhdh;hhgt;hhhg" "Bctr;hhhhdj;gg;dd"   
#[4] "Bctr;hhhg;ggj"        "Bctg;hhgg;hhj"  
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With