You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何移除特定字符串「? - 」之前的所有内容(包含自身)

问题原因

你之前使用的.*-正则规则是贪婪匹配模式,会从字符串开头一直匹配到整段文本中最后一个连字符-的位置,所以T-tests里的连字符也会被命中,导致多删除了不该删的内容。

解决方案

直接匹配你需要的完整分隔组合? - 即可,注意?在正则语法中属于特殊字符,需要用双反斜杠转义才能匹配到字面的问号:

gsub(".*\\? - ", "", "To what extent do you disagree or agree with the following statements? - Statistics make me cry.")
gsub(".*\\? - ", "", "To what extent do you disagree or agree with the following statements? - T-tests are easy to interpret.")

运行后输出结果:

[1] "Statistics make me cry."
[1] "T-tests are easy to interpret."

如果你的文本存在换行场景,想要确保跨换行匹配,可以开启Perl正则模式并添加单行修饰符:

gsub("(?s).*\\? - ", "", 你的字符串变量, perl = TRUE)

内容的提问来源于stack exchange,提问作者writer_typer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.18 01:36:03