我有一个字符串向量,如下所示:
x <- c("gene_biotype \"protein_coding\"; transcript_name \"IGHV3-66-201\";
transcript_source \"havana\"; transcript_biotype \"IG_V_gene\";
protein_id \"ENSP00000375041\"; protein_version \"2\"; tag
\"cds_end_NF\"; tag \"mRNA_end_NF\"; tag \"basic\";
transcript_support_level \"NA\";",
"gene_id \"ENSG00000211973\"; gene_version \"2\"; transcript_id
\"ENST00000390633\"; transcript_version \"2\"; exon_number \"1\";
gene_name \"IGHV1-69\"; gene_source \"ensembl_havana\"; gene_biotype
\"IG_V_gene\"; transcript_name \"IGHV1-69-201\"; transcript_source
\"ensembl_havana\"; transcript_biotype \"IG_V_gene\"; protein_id
\"ENSP00000375042\"; protein_version \"2\"; tag \"cds_end_NF\"; tag
\"mRNA_end_NF\"; tag \"basic\"; transcript_support_level \"NA\";",
"gene_id \"ENSG00000211973\"; gene_version \"2\"; transcript_id
\"ENST00000390633\"; transcript_version \"2\"; exon_number \"2\";
gene_name \"IGHV1-69\"; gene_source \"ensembl_havana\"; gene_biotype
\"protein_coding\";")
我需要提取gene_biotype后面的引号文本(任何字符)。例如:
[1] protein_coding\
[2] IG_V_gene\
[3] protein_coding\
我已经尝试在stringr包中使用str_extract,但是我无法使正则表达式工作。
任何帮助都将不胜感激!
发布于 2019-04-16 01:25:41
您可以在stringr
包的帮助下使用正则表达式来获取所需的数据。例如
library(stringr)
str_match(x, "gene_biotype\\s+\"([^\"]+)\"")
# [,1] [,2]
# [1,] "gene_biotype \"protein_coding\"" "protein_coding"
# [2,] "gene_biotype \n\"IG_V_gene\"" "IG_V_gene"
# [3,] "gene_biotype \n\"protein_coding\"" "protein_coding"
这将返回一个包含匹配和类别的矩阵。如果你只是想要这个类别,你可以做
str_match(x, "gene_biotype\\s+\"([^\"]+)\"")[,2]
# [1] "protein_coding" "IG_V_gene" "protein_coding"
发布于 2019-04-16 01:31:32
我找到了这个here
stringi::stri_extract_all_regex(x, '(?<=").*?(?=")')[[1]][1]
#[1] "protein_coding"
https://stackoverflow.com/questions/55694298
复制相似问题