gsub比赛后第一个字

时间:2019-08-30 07:55:51

标签: r regex

我只需要替换比赛后的第一个单词。但是比赛之后所有的单词都会被替换

下面的代码替换了所有单词,但是我只需要删除Vehicle Details : VolvoVehicle Details : scania_z而不是比赛之后的所有单词

我尝试的代码:

ad<-c("Pl. find the Vehicle Details : Volvo needs to be repaired","The vehicle number : 4570d having the Vehicle Details : scania_z should be altered")
s<-gsub("Vehicle Details :*","",ad)
s

预期结果是:

[1] "Pl. find the needs to be repaired"                         
[2] "The vehicle number : 4570d having the should be altered"

1 个答案:

答案 0 :(得分:3)

您可以使用

Dim devInfo As DeviceInfoType = Nothing
Dim devInfo As New DeviceInfoType

gsub("Vehicle Details\\s*:\\s*\\w+","",ad)

请参见R demo

gsub("Vehicle Details\\s*:\\s*\\S+","",ad) 模式匹配1+个字母,数字或下划线,而\w+模式匹配1+个非空白字符。如果您要保留的单词的末尾带有标点符号,则最好使用第一个,但是如果您只想删除\S+之后的所有非空白字符,则最好使用后者。