Gsub是R中关键字后面的每个元素

eig*_*foo 3 string r string-substitution gsub

我想在某个关键字后删除字符串的所有元素.示例:

this.is.an.example.string.that.I.have
Run Code Online (Sandbox Code Playgroud)

期望的输出:

This.is.an.example
Run Code Online (Sandbox Code Playgroud)

我尝试过使用gsub('string', '', list)但只删除单词字符串.我也试过使用,gsub('^string', '', list)但似乎也没有用.

谢谢.

Rav*_*h13 5

以下简单sub可以帮到你.

sub("\\.string.*","",variable)
Run Code Online (Sandbox Code Playgroud)

说明:使用方法sub

sub(regex_to_replace_text_in_variable,new_value,variable)
Run Code Online (Sandbox Code Playgroud)

sub和之间的区别gsub:

sub:用于执行变量替换.

gsub:gsub仅用于相同的替换任务,但只会在找到的所有匹配项上执行替换,但仅在找到sub的第一个匹配项中执行它.

从帮助页面 R:

sub(pattern,replacement,x,ignore.case = FALSE,perl = FALSE,fixed = FALSE,useBytes = FALSE)

gsub(pattern,replacement,x,ignore.case = FALSE,perl = FALSE,fixed = FALSE,useBytes = FALSE)