简体   繁体   English

如何在字符串中选择两个字符?

[英]How can I select two characters in a string?

I know that maybe is something of very easy to resolve but, looking for various example online, I did not find the right example to resolve my problem. 我知道这也许很容易解决,但是在网上寻找各种示例时,我没有找到解决问题的正确示例。

I have in a data.frame under a column a following phrase: 我在列下的data.frame中有以下短语:

ID
p_IIJSJ;o_OODJ;l_jjjjw;g_jjjdI
p_HHDU;o_WWj;l_WWOJ;g_jjjDI

I would like to select two words: the one who start with p_ and the one who start with g_ and eliminate all the rest which is between them.... do you have any suggestion about how make it? 我想选择两个词:一个以p_开头,一个以g_开头,并消除它们之间的所有其余词。...您对此有何建议? I'm trying with gsub but with no success at the moment. 我正在尝试使用gsub但目前没有成功。 Thank you a lot in advance 提前多谢

An approach with strrsplit , 一种strrsplit的方法,

sapply(strsplit(x, ';'), function(i) paste(grep('p_|g_', i, value = TRUE), collapse = ';'))
#[1] "p_IIJSJ;g_jjjdI"

or if the order is always the same (as @Jaap mentions) 或顺序始终相同(如@Jaap所述)

sapply(strsplit(df$ID,';'), function(x) paste(x[c(1,4)], collapse=';'))

I suggest you use package stringr which makes it easy: 我建议您使用stringr package stringr

library(stringr)

a <- "p_IIJSJ;o_OODJ;l_jjjjw;g_jjjdI"
b <- "p_HHDU;o_WWj;l_WWOJ;g_jjjDI"

str_extract(string = a, pattern = c("p_[a-zA-Z]+", "g_[a-zA-Z]+"))

# [1] "p_IIJSJ" "g_jjjdI"

str_extract(string = b, pattern = c("p_[a-zA-Z]+", "g_[a-zA-Z]+"))

# [1] "p_HHDU"  "g_jjjDI"

We can use sub 我们可以使用sub

sub(";*(p_\\w+).*;*(g_\\w+).*", "\\1;\\2", df1$ID)
#[1] "p_IIJSJ;g_jjjdI" "p_HHDU;g_jjjDI" 

Or with gsub 或搭配gsub

gsub("[^pg]_\\w+;", "", df1$ID)
#[1] "p_IIJSJ;g_jjjdI" "p_HHDU;g_jjjDI" 

data 数据

df1 <- structure(list(ID = c("p_IIJSJ;o_OODJ;l_jjjjw;g_jjjdI", "p_HHDU;o_WWj;l_WWOJ;g_jjjDI"
)), .Names = "ID", class = "data.frame", row.names = c(NA, -2L))

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

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