Remove everything between specific characters

Viewed 48

I have a string that looks like this:

BA46_05-07-2019_rxn2_AAGAACATCCCTCTCC

I want to remove everything between rxn2 and BA46_

Desired end results:

BA46_rxn2_AAGAACATCCCTCTCC

How can I do this?

4 Answers
gsub("BA46_[a-zA-Z0-9_-]*rxn2","BA46_rx2","BA46_05-07-2019_rxn2_AAGAACATCCCTCTCC")

Try this

x <- "BA46_05-07-2019_rxn2_AAGAACATCCCTCTCC"

sub("_[0-9\\-]*_" , "_" , x)
  • output
[1] "BA46_rxn2_AAGAACATCCCTCTCC"
library(stringr)

str_remove(string, '\\d{2}\\-\\d{2}\\-\\d{4}\\_')

[1] "BA46_rxn2_AAGAACATCCCTCTCC"
sub('(.*BA46_).*(rxn2.*)','\\1\\2', 'BA46_05-07-2019_rxn2_AAGAACATCCCTCTCC')

I would go with this because it

  1. does not depend on what is being deleted (which is crucial IMHO)
  2. only contains BA46_ and rxn2 once, thus reducing the room for error
Related