Karan Pande
Karan Pande

Reputation: 13

Regular Expression using R Programming Language

String<- "46,XX,t(1;19)(p32;q13.3),t(6;9)(p22;q34),del(32)t(12;16)(p12;q21)[cp20]"

The value I want to extract is t(1;19)(p32;q13.3), t(6;9)(p22;q34), t(12;16)(p12;q21)

The regex I'm using

ABC<-str_extract(String, regex("t.{1,16}"))

output I Get: t(1;19)(p32;q13.3

I know my code I incomplete but I'm unable to figure out a way to extract this information.

Thank you in advance

Upvotes: 0

Views: 62

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 388817

Assuming your String is :

String<- "46,XX,t(1;19)(p32;q13.3),t(6;9)(p22;q34),del(32)t(12;16)(p12;q21)[cp20]"

We can use str_extract_all as :

stringr::str_extract_all(String, "t\\(.*?\\)\\(.*?\\)")[[1]]
#[1] "t(1;19)(p32;q13.3)" "t(6;9)(p22;q34)"    "t(12;16)(p12;q21)" 

This returns "t" followed by everything in round brackets (()), followed by everything in another round bracket next to it.

Upvotes: 1

Related Questions