r - 使用正则表达式来阻止单词
问题描述
我正在尝试使用正则表达式来阻止文本中的单词。
c <- "Foo is down. No one wants Foos after this. Before, people liked Fooy a lot."
期望的输出:
"Foo is down. No one wants Foo after this. Before, people liked Foo a lot."
我需要保留单词Foo
,但删除该单词后面的所有字符,保留字符串的其余部分。
我设法将后缀从单词的基础中分离出来,我可以在单词“Foo”的变体之后删除所有内容,并且我尝试了单词边界,但无法弄清楚如何获得所需的输出。
解决方案
我们可以尝试使用空字符串gsub
替换模式:(?<=Foo)\S+
x <- "Foo is down. No one wants Foos after this. Before, people liked Fooy a lot."
output <- gsub("(?<=Foo)\\S+", "", x, perl=TRUE)
output
[1] "Foo is down. No one wants Foo after this. Before, people liked Foo a lot."
演示
推荐阅读
- cmake - 如何在 cmake 中使用浮动 WindRiver 许可证
- java - 重定向所有 html 获取请求以从 ROOT/frontEnd 文件夹而不是 Tomcat 服务器上的 ROOT 获取文件
- ios - 滑动以从通知中心关闭通知时,有什么方法可以获得回调或处理程序?
- docker - Pyomo 在 Docker 上使用 Flask - 找不到求解器“glpk”的可执行文件
- laravel - Laravel Rest API 在 reqbin.com 中工作,但不在邮递员中,也不能在应用程序中工作
- date - 批处理文件以查找当前日期(日月)并相应地显示结果
- algorithm - 算法复杂度,3 个循环
- c# - 分区:如何在每个分区后添加等待
- c# - 将图像转换为 numpy.net 数组
- python - 如何保存可学习的张量流分布?