r - R如何在特定单词之后提取所有段落中的所有内容?
问题描述
您好,我正在寻找一个 R 代码来删除特定术语之后所有段落中的每个单词。寻找“谈话:”并替换所有内容直到新段落的示例。我尝试了正则表达式并花时间但无法成功(“fjeaofiz”始终存在)。
x <- c("12 3456 789", "Talk: zpfozefpozjgzigzehgoi oezjgzogzjgoezjgo \r fjeaofiz ", "", NA, "Talk: 667")
stri_sub_all(x, stri_locate_all_regex(x, "^Talk:.*\r", omit_no_match=TRUE)) <- "***"
print(x)
我的输出应该是:
x <-"12 3456 789", "***", "", NA, "***"
有什么帮助吗?
解决方案
You need to use
stri_sub_all(x, stri_locate_all_regex(x, "(?s)^Talk:.*", omit_no_match=TRUE)) <- "***"
The point here is to remove \r
(your regex matched only the part of the line until CR char) and use (?s)
with .*
pattern to match the rest of the whole string, because stringi
regex package uses ICU regex flavor and .
does not match line break chars (like CR and LF) by default. (?s)
enables .
to match line breaks.
Probably a simpler approach is to use
sub("^Talk:.*", "***", x)
Here, the default TRE regex library is used and .
matches line breaks by default in this regex flavor.
推荐阅读
- docker - 如何在不使用 docker 命令的情况下将 docker 镜像推送到 nexus3 存储库?
- reactjs - 在 React Router 中使用自定义路由器开关
- c++ - 一个无效参数被传递给一个认为无效参数致命的函数?
- python - 我是否将此 MATLAB 模拟波形代码正确转换为 Python?
- groovy - 如何在groovy IF语句中将表达式与字符串进行比较
- ios - 如果顶部有标题视图,如何在 SwiftUI 列表下正确显示数据
- javascript - 如果用户单击正文内的任何位置,如何关闭搜索框?
- mysql - 如何编写一个查询,将一个表的每一行与 MySQL 中不同表的所有行进行比较?
- windows - %~dp0 在尝试从 .txt 文件接收内容但在其他 .bat 文件中工作时不起作用?
- flutter - Flutter:运行 pub get 时 image_picker 失败