正则表达式 – R列表字符删除

lst1是一个列表:

lst1 <- list(c("all the apples","apples in the tables","fashion prosthetics"),c("meteorological concepts","effects of climate change","environmental"))

我想保留列表结构并从所有单词中删除最后一个.所需答案如下:

> lst1
[[1]]
[1] "all the apple"      "apple in the table" "nature"            

[[2]]
[1] "meteorological concept"   "effect of climate change"
[3] "environmental"

我试过了

gsub("\\'s|s$|s[[:space:]]{0}","",lst1)

但它不保留列表结构.

怎么做到呢?

解决方法

您可以使用带有lapply的gsub循环遍历列表元素

lapply(lst1,gsub,pattern= "\\'s|s$|s\\b",replacement='')
#[[1]]
#[1] "all the apple"       "apple  in the table" "fashion prosthetic" 

#[[2]]
#[1] "meteorological concept"    "effect  of climate change"
#[3] "environmental"

相关文章

正则替换html代码中img标签的src值在开发富文本信息在移动端...
正则表达式
AWK是一种处理文本文件的语言,是一个强大的文件分析工具。它...
正则表达式是特殊的字符序列,利用事先定义好的特定字符以及...
Python界一名小学生,热心分享编程学习。
收集整理每周优质开发者内容,包括、、等方面。每周五定期发...