如何删除表达式前面的字符串中不需要的单词?

时间:2018-09-13 10:56:54

标签: r regex

我试图仅使用R中的正则表达式来提取具有选定单词的字符串。

例如:

SLIM SSD POWER ADAPTER
  8.4L 1ST 3.5 HDD POWER ADAPTER
  SATA POWER ADAPTER FOR ODD/2HDD
  SLIM SSD SATA&POWER ADAPTER
  SATA POWER ADAPTER FOR HDD
  2ND 3.5 HDD+SSD POWER ADAPTER
  SATA POWER ADAPTER
  1ND 3.5 HDD+SSD POWER ADAPTER
  DUAL NET CARD SATA POWER ADAPTER
 POWER ADAPTER (Others)

我想提取任何带有“ POWER ADAPTER”前缀或后缀的东西,希望上面的字符串是“ DUAL NET CARD SATA POWER ADAPTER”,  我尝试使用负向后看,仍然无法获得所需的提取

谢谢

1 个答案:

答案 0 :(得分:0)

您可以将 0 => '12v some' 1 => 'Item, which contains comma' 2 => 'Another item' 函数与grep参数一起使用,如下所示:

value = TRUE

输出:

strs <- c("SLIM SSD POWER ADAPTER", "POWER CORD", "LEAST SQUARE", "8.4L 1ST 3.5 HDD POWER ADAPTER", "SATA POWER ADAPTER FOR ODD/2HDD", "SLIM SSD SATA&POWER ADAPTER", 
"SATA POWER ADAPTER FOR HDD", "2ND 3.5 HDD+SSD POWER ADAPTER", 
"SATA POWER ADAPTER", "1ND 3.5 HDD+SSD POWER ADAPTER", "DUAL NET CARD SATA POWER ADAPTER", "POWER ADAPTER (Others)", "RICHIE", "MONITOR")

grep("POWER ADAPTER", strs, value = TRUE)