首页 > 解决方案 > 删除字符串中特定字符后的 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!"

但诚然,我对正则表达式非常不满意。有人可能有任何想法吗?谢谢!

标签: rregex

解决方案


您可以尝试(?<=\\.)\\d+匹配句号后的数字,例如,

> gsub("(?<=\\.)\\d+", "", s, perl = TRUE)
[1] "There were 100 people outside. They were sharing 10 hotdogs. Nice!"

一种更有效的方法(感谢@JvdV 的评论)可能是

gsub("\\.\\d+", ".", s, perl = TRUE)

推荐阅读