简体   繁体   English

R:如何提取向量的两个字符之间的确切字符

[英]R:how to extract the exact character between two characters for a vector

dat1 <- c('human(display_long)|uniprotkb:ESR1(gene name)')
dat2 <- c('human(display_long)|uniprotkb:TP53(gene name)')
dat3 <- c('human(display_long)|uniprotkb:GPX4(gene name)')
dat4 <- c('human(display_long)|uniprotkb:ALOX15(gene name)')
dat5 <- c('human(display_long)|uniprotkb:PGR(gene name)')
dat <- c(dat1,dat2,dat3,dat4,dat5)

how to extract the gene name between 'human(display_long)|uniprotkb:' and '(gene name)' for vector dat.Thanks!如何提取载体 dat 的 'human(display_long)|uniprotkb:' 和 '(gene name)' 之间的基因名称。谢谢!

You can use regexpr and regmatches to extract the text between human(display_long)|uniprotkb: and (gene name) .您可以使用regexprregmatches提取human(display_long)|uniprotkb:(gene name)之间的文本

regmatches(dat
 , regexpr("(?<=human\\(display_long\\)\\|uniprotkb:).*(?=\\(gene name\\))"
 , dat, perl=TRUE))
#[1] "ESR1"   "TP53"   "GPX4"   "ALOX15" "PGR"   

Where (?<=human\\\\(display_long\\\\)\\\\|uniprotkb:) is a positive look behind for human(display_long)|uniprotkb: and (?=\\\\(gene name\\\\) is a positive look ahead for (gene name) and .* is the text in between.其中(?<=human\\\\(display_long\\\\)\\\\|uniprotkb:)是对human(display_long)|uniprotkb:的正面展望,而(?=\\\\(gene name\\\\)是对human(display_long)|uniprotkb:的正面展望(gene name).*是中间的文本。

Another way is to use sub but this might fail in case there is no match.另一种方法是使用sub但如果没有匹配,这可能会失败。

sub(".*human\\(display_long\\)\\|uniprotkb:(.*)\\(gene name\\).*", "\\1", dat)
#[1] "ESR1"   "TP53"   "GPX4"   "ALOX15" "PGR"   

Other ways not searching for the full pattern might be:其他不搜索完整模式的方法可能是:

regmatches(dat, regexpr("(?<=:)[^(]*", dat, perl=TRUE))
sub(".*:([^(]*).*", "\\1", dat)
sub(".*:(.*)\\(.*", "\\1", dat)

You can try this regex which will extract the text between 'uniprotkb' and opening round brackets ( ( ).您可以尝试使用此正则表达式,它将提取'uniprotkb''uniprotkb'括号 ( ( ) 之间的文本。

sub('.*uniprotkb:(\\w+)\\(.*', '\\1', dat)
#[1] "ESR1"   "TP53"   "GPX4"   "ALOX15" "PGR"  

Using stringr and look behind you could try this:使用stringr并查看后面你可以试试这个:

library(stringr)
str_extract(dat, "(?<=:)[A-z0-9]+")
#[1] "ESR1"   "TP53"   "GPX4"   "ALOX15" "PGR"   

Assuming that there is only one colon which precedes the gene name.假设在基因名称之前只有一个冒号。

We can use str_remove_all我们可以使用str_remove_all

library(stringr)
str_remove_all(dat, ".*uniprotkb:|\\(.*")
[1] "ESR1"   "TP53"   "GPX4"   "ALOX15" "PGR"   

Or use trimws from base R或者使用来自base R trimws

trimws(dat, whitespace = ".*uniprotkb:|\\(.*")
[1] "ESR1"   "TP53"   "GPX4"   "ALOX15" "PGR"   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM