regex - 提取 | 之间的最后一个单词|

标签 regex r stringr

我有以下数据集

> head(names$SAMPLE_ID)
[1] "Bacteria|Proteobacteria|Gammaproteobacteria|Pseudomonadales|Moraxellaceae|Acinetobacter|"
[2] "Bacteria|Firmicutes|Bacilli|Bacillales|Bacillaceae|Bacillus|"                            
[3] "Bacteria|Proteobacteria|Gammaproteobacteria|Pasteurellales|Pasteurellaceae|Haemophilus|" 
[4] "Bacteria|Firmicutes|Bacilli|Lactobacillales|Streptococcaceae|Streptococcus|"             
[5] "Bacteria|Firmicutes|Bacilli|Lactobacillales|Streptococcaceae|Streptococcus|"             
[6] "Bacteria|Firmicutes|Bacilli|Lactobacillales|Streptococcaceae|Streptococcus|" 

我想提取 || 之间的最后一个单词作为新变量,即

Acinetobacter
Bacillus
Haemophilus

我尝试过使用

library(stringr)
names$sample2 <-   str_match(names$SAMPLE_ID, "|.*?|")

最佳答案

我们可以使用

library(stringi)
stri_extract_last_regex(v1, '\\w+')
#[1] "Acinetobacter"

数据

v1 <- "Bacteria|Proteobacteria|Gammaproteobacteria|Pseudomonadales|Moraxellaceae|Acinetobacter|"

关于regex - 提取 | 之间的最后一个单词|,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34342380/

相关文章:

regex - 使用正则表达式从给定单词提取文本直到段落末尾

r - 使用ggplot2将构面标签中的新行和斜体组合

r - 如何在R中的数据框中获取列表的最大值

r - `paste`, `str_c`, `str_join`, `stri_join`, `stri_c`和 `stri_paste`之间的区别?

java - 用于替换的字符串正则表达式

python - 如何让re.search寻找多种模式?

java - Java 日期验证的正则表达式

r - 提取数据帧每行中第一个非 NA 值的列名称

python - python中的隔离森林算法

r - 在两个不同的向量上匹配相同的字符串