r - 删除字符串中特定字符后的 n 个字符
问题描述
我试图弄清楚如何使用 R 以下列方式从字符串中删除引文编号:
原始字符串:
"There were 100 people outside.231 They were sharing 10 hotdogs.42 Nice!"
所需字符串:
"There were 100 people outside. They were sharing 10 hotdogs. Nice!"
但诚然,我对正则表达式非常不满意。有人可能有任何想法吗?谢谢!
解决方案
您可以尝试(?<=\\.)\\d+
匹配句号后的数字,例如,
> gsub("(?<=\\.)\\d+", "", s, perl = TRUE)
[1] "There were 100 people outside. They were sharing 10 hotdogs. Nice!"
一种更有效的方法(感谢@JvdV 的评论)可能是
gsub("\\.\\d+", ".", s, perl = TRUE)
推荐阅读
- node.js - 如何将测试数据从生产环境导入 Firebase Firestore 模拟器以便于测试?
- python - 如何制作自定义错误页面 django 2.2
- python - 这三个选项中哪一个对矩阵方程“Wx + b”最有效?
- python - 如何摆脱 bw 图像中的点并留下连接的像素?
- vue.js - 你好 !我正在使用 vue js 创建一个 ipl 仪表板
- telegram - 关注电报组中用户的消息
- python - 一起使用 Pycharm 和 Spyder
- r - 如何从一组选定的列中删除 na?
- django - 如何计算不同模型中外键的所有实例?
- css - Bootstrap 4:导航栏导航项目下划线