Workhorse
Workhorse

Reputation: 1560

Remove everything between specific characters

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?

Upvotes: 1

Views: 56

Answers (4)

Robert Hacken
Robert Hacken

Reputation: 4725

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

Upvotes: 0

TarJae
TarJae

Reputation: 78917

library(stringr)

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

[1] "BA46_rxn2_AAGAACATCCCTCTCC"

Upvotes: 0

Mohamed Desouky
Mohamed Desouky

Reputation: 4425

Try this

x <- "BA46_05-07-2019_rxn2_AAGAACATCCCTCTCC"

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

Upvotes: 0

Eric
Eric

Reputation: 1389

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

Upvotes: 2

Related Questions