r - 从句子中删除单个字符
问题描述
我是字符串来清理一些包含单个字符的句子,例如:
sentence <- c('this is a z test', 'remove this b from here')
我想 从句子中删除z
and以拥有:b
c('this is a test', 'remove this from here')
我尝试过这样的事情:
gsub('"([\\b[a-zA-Z0-9]{1,1}\\b])"', '', sentence)
但它不工作。
解决方案
我们可以使用gsub
with word boundary ( \\b
)
gsub("\\s\\b[zb]\\b", "", sentence)
#[1] "this is a test" "remove this from here"
如果要删除“a”或“i”以外的值,请^
在方括号内指定
gsub("\\s\\b[^ai]\\b", "", sentence)
推荐阅读
- python - 为尺寸为 250*250 的图像添加边框以制作 400*400
- python - 将返回的元素连接到递归函数中的列表
- python - 如何在python中找到最大值和重合日
- elasticsearch - ElasticSearch:结果窗口太大
- html - 使用 SVG 作为具有 position: absolute 的灵活背景;
- node.js - 使用带有 findOne() 方法的 NodeJS 从 MongoDB 数据库获取服务器端变量信息的问题
- c - 如何根据桶大小更改除法哈希函数
- javascript - 如何等待数组完成,然后执行函数?
- java - 如何使用 SpringBoot 2 运行单元测试
- c# - 为什么我不能在 Session 中保存对象